linux bash find file name with several digits - linux

I have a folder containing files jobs_1, jobs_2 ... jobs_77. I used
find . -name "job_[0-9]{1,2}" -type f
to list these files, but got no output. What is the problem?

Or, to be friendly:
find . -regextype posix-awk -regex ".*job_[0-9]{1,2}.*" -type f
Note: posix-extended for example will yield the same result in this case; but being inherently lazy I chose awk because it's shorter :}

The first problem with the command in the question:
find .-name "job_[0-9]{1,2}" -type f
...is that you are searching for files that start with the string jobs_ instead of job_ so the above command will not find any of the files named files jobs_1, jobs_2 ... jobs_77.
The correct command to use is:
find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f -exec basename {} \;
-regextype posix-awk - This option changes the regular expression syntax understood by -regex which allows the find program to use posix-awk regular expressions.
-exec basename {} \; - Converts the results of find . -regextype posix-awk -regex ".*jobs_[0-9]{1,2}.*" -type f from the full path of the files to the file names, so that only the file names are displayed in the results. This makes the results of find -type f with the -regex option similar to the results of find -type f without the -regex option in which only the file names are displayed.

Related

How to find possible unquoted pattern after predicate `-exec'?

I have 5 files that I want to unrar(and I have done it many times):
find . -name "*.rar"
./Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part5_Downloadly.ir.rar
./Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part2_Downloadly.ir.rar
./Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part4_Downloadly.ir.rar
./Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part1_Downloadly.ir.rar
./Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part3_Downloadly.ir.rar
My command
find . -name "*.rar" -exec unrar x -o+ \; Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part1_Downloadly.ir.rar
I got error
find: paths must precede expression: `Udemy_Kubernetes_Mastery_Hands-On_Lessons_From_A_Docker_Captain_2020-4.part1_Downloadly.ir.rar'
find: possible unquoted pattern after predicate `-exec'?
Why?
Change your command into:
find . -name "*.rar" -exec unrar x -o+ {} \;
Where {} represents the file found by the find command.

How do i find all file Which as name String+Number to it Linux

I have to find all files whose name start with String + Number like below
ABC123_filedemo.txt
AB_451_filetxt
CD_789_demo.txt
demo_files_FD123.txt
d_files_re_SD_456.txt
I have tried this Command But Not working
export _date=`date "+%d_%m_%Y_%H_%M_%S"`
find . -type f -iname 'AB*' -exec mv {} /Demo_files"_"$_date \;
Is ".txt" relevant? Then try:
find ./ -regextype posix-extended -regex '.*[a-zA-Z]+.*[0-9]+\.txt' -exec <your stuff>
inspired by: https://stackoverflow.com/a/5249797/10514446
This looks like a simple example:
find ./ -name "[a-zA-Z]*[0-9]*"
Every file here needs to start with a letter 'a'-'z' (small or large caps) and somewhere in the filename you need a digit [0-9].

Inserting text in a 'find' command search

I have a find string that finds all the instances of a particular filename in a path, like so:
find /opt/logs* -type f -name "deploy.log" -exec ls {} \;
I need to return the result with 'FINENAME=' prepended on each line. Having a hard time figuring the best way.
find /opt/logs* -type f -name deploy.log | sed 's/^/FILENAME=/'
Note that if you have a directory named /opt/logs (and you're not trying to look in /opt/logs-foo/ and /opt/logs-date, or the like) you can drop the * and write find /opt/logs -type ...
Use the -printf option:
find /opt/logs* -type f -name "deploy.log" -printf='FILENAME=%p\n'
%p will get expanded to the file's name.

Find all files contained into directory named

I would like to recursively find all files contained into a directory that has name “name1” or name “name2”
for instance:
structure/of/dir/name1/file1.a
structure/of/dir/name1/file2.b
structure/of/dir/name1/file3.c
structure/of/dir/name1/subfolder/file1s.a
structure/of/dir/name1/subfolder/file2s.b
structure/of/dir/name2/file1.a
structure/of/dir/name2/file2.b
structure/of/dir/name2/file3.c
structure/of/dir/name2/subfolder/file1s.a
structure/of/dir/name2/subfolder/file2s.b
structure/of/dir/name3/name1.a ←this should not show up in the result
structure/of/dir/name3/name2.a ←this should not show up in the result
so when I start my magic command the expected output should be this and only this:
structure/of/dir/name1/file1.a
structure/of/dir/name1/file2.b
structure/of/dir/name1/file3.c
structure/of/dir/name2/file1.a
structure/of/dir/name2/file2.b
structure/of/dir/name2/file3.c
I scripted something but it does not work because it search within the files and not only folder names:
for entry in $(find $SEARCH_DIR -type f | grep 'name1\|name2');
do
echo "FileName: $(basename $entry)"
done
If you can use the -regex option, avoiding subfolders with [^/]:
~$ find . -type f -regex ".*name1/[^/]*" -o -regex ".*name2/[^/]*"
./structure/of/dir/name2/file1.a
./structure/of/dir/name2/file3.c
./structure/of/dir/name2/subfolder
./structure/of/dir/name2/file2.b
./structure/of/dir/name1/file1.a
./structure/of/dir/name1/file3.c
./structure/of/dir/name1/file2.b
I'd use -path and -prune for this, since it's standard (unlike -regex which is GNU specific).
find . \( -path "*/name1/*" -o -path "*/name2/*" \) -prune -type f -print
But more importantly, never do for file in $(find...). Use finds -exec or a while read loop instead, depending on what you really need to with the matching files. See UsingFind and BashFAQ 20 for more on how to handle find safely.

how to exclude few folder levels in the FIND command results - unix

Following is the folder structure
- home/ABCD/test1/example1/sample1/textfile.txt
If I execute the find command like
find /home/ABCD/ -type f -print
I am getting the following output
/home/ABCD/test1/example1/sample1/textfile.txt
Note: I am executing the find command from the ABCD folder, In the results I want to exclude /home/ABCD/ folder I just want /test1/example1/sample1/testfile.txt as the result
How can I achieve this?
Since you are executing find from /home/ABCD/ do something like this:
find * -type f -print
Or if you are looking for files in test1 do this:
find test1 -type f -print
Also with -maxdepth N you can limit the recursion in find
If you only want to look for files named textfile.txt do
find test1 -type f -name 'textfile.txt' -print
If you want to print the leading slash do
find . -type f -printf '/%p\n'
For more info have a look here
Note: If have the above string in a variable, you can trim it like this:
string="/home/ABCD/test1/example1/sample1/textfile.txt"
echo "${string#/home/ABCD}"
Some more examples of string manipulation here
Just use . as the starting directory
find . -type f -print
gives
./test1/example1/sample1/textfile.txt
and if you really want a leading slash, use -printf for the output
find . -type f -printf '/%P\n'
You can use the mindepth parameter to start looking at one level below the current directory
find /home/ABCD/ -mindepth 1 -type f -print
This should substitute your current working directory name with a .
find . -type f | perl -pne "s#$PWD#.#"
So you would get results like:
./test1/example1/sample1/textfile.txt
If you do not want the preceeding ./, use this command instead:
find . -type f | perl -pne "s#$PWD/##"

Resources