Find file without spaces in file name - linux

I'm trying to find all *.cpp, using find files under current directory, which do not contain spaces in neither dirname nor basename. I understand I need to use -wholename flag, but I can not find an appropriate regex syntax.

Use find with a regex:
find . -type f -regex "[^ ]*.cpp"

Related

How do I find a file containing using wildcard on Linux?

I would like to know if it is possible to use find with wildcards:
I use this command, but I have an error
find -type f -name /target/*.zip
You need to put the wildcard in quotes, otherwise it gets expanded by the shell before the command is run.
And it should just be a filename, not a pathname. The directory to start searching should be an argument to find before the filter specifications.
find /target -type f -name '*.zip'

find command regex with optional subexpression

I have the following file list
file <-
file.2019041543764832 <-
file.2019041643764832 <-
file.2019041243764832
file.2019041143764832
I want to find all the marked files which are prefixed with file and optionally suffixed by the dates 20190415xxxxx or 20190416xxxxx
I have tried the following but it does not yield any output.
find . -regex 'file(\.2019041(5|6)[0-9].*)?' -regextype egrep
I need some help with the correct regex type and the correct synatx to achieve this.
find . -regex './file\(.2019041\(5\|6\)[0-9]*\)?' -regextype egrep
or just
find . -regex './file\(.2019041[56][0-9]*\)?'
(When using find ., my version of find prefixes the matches with ./, so I added that to the regexp.)

Mass Find/Replace within files having specific filename under command line

I am looking for a quick command to search all .htaccess files for a specific IP address and change it to another IP address from the command line
something like
grep -rl '255.255.254.254' ./ | xargs sed -i 's/254/253/g'
I know the above example is a bad way to do it, just an example (and showing I did some searching to find a solution
Search: files with filename .htaccess (within 2 levels deep of current path?)
Find: 255.255.254.254
Replace with: 255.255.253.253
or, is this too much to ask of my server and I would be better off replacing them as I find them?
Try:
find . -type f -name '.htaccess' -execdir sed -i 's/255\.255\.254\.254/255.255.253.253/g' {} +
How it works:
find .
Start looking for files in the current directory.
-type f
Look only for regular files.
-name '.htaccess'
Look only for files named .htaccess.
-execdir sed -i 's/255\.255\.254\.254/255.255.253.253/g' {} +
For any such files found, run this sed command on them.
Because . is a wildcard and you likely want to match only literal periods, we escape them: \.
We use -execdir rather than the older -exec because it is more secure against race conditions.

find type -f also returns non-matching files in matching directory

all files/folders in current directory:
./color_a.txt
./color_b.txt
./color_c.txt
./color/color_d.txt
./color/blue.txt
./color/red.txt
./color/yellow.txt
command used to find all files with the word color in name:
find ./*color* -type f
result:
./color_a.txt
./color_b.txt
./color_c.txt
./color/color_d.txt
./color/blue.txt
./color/red.txt
./color/yellow.txt
expected result:
./color_a.txt
./color_b.txt
./color_c.txt
./color/color_d.txt
The result also includes all the non-matching file names under a matching parent directory.
How could I get ONLY files with names directly matching the color pattern?
Thanks a lot!
What you probably want for filename filtering is a simple -name <glob-pattern> test:
find -name '*color*' -type f
From man find:
-name pattern
Base of file name (the path with the leading directories removed) matches shell
pattern pattern. Because the leading directories are removed, the file names
considered for a match with -name will never include a slash, so `-name a/b' will
never match anything (you probably need to use -path instead).
Just as a side note, when you wrote:
find ./*color* -type f
the shell expanded the (unquoted) glob pattern ./*color*, and what was really executed (what find saw) was this:
find ./color ./color_a.txt ./color_b.txt ./color_c.txt -type f
thus producing a list of files in all of those locations.
You can use the regex option
find -regex ".*color_.*" -type f

How to recursively delete all files in folder that dont match a given pattern

I would like to delete all files in a given folder that dont match the pattern ^transactions_[0-9]+
Let's say I have these files in the folder
file_list
transactions_010116.csv
transactions_020116.csv
transactions_check_010116.csv
transactions_check_020116.csv
I would like to delete transactions_check_010116.csv and transactions_check_020116.csv and leave the first two as they are using ^transactions_[0-9]+
I've been trying to use find something like below, but this expression deletes everything in the folder not just the files that dont match the pattern:
find /my_file_location -type f ! -regex '^transactions_[0-9]+' -delete
What i'm trying to do here is using regex find all files in folder that dont start with ^transactions_[0-9]+ and delete them.
Depending on your implementation, you could have to use option -E to allow the use of full regexes. An other problem is that -regex gives you an almost full path starting with the directory you passed.
So the correct command should be:
find -E /my_file_location ! -regex '.*/transactions_[0-9]+$' -type f -delete
But you should first issue the same with -print to be sure...
grep has -v option to grep everything not matching the provided regex:
find . | grep -v '^transactions_[0-9]+' | xargs rm -f

Resources