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

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.

Related

linux bash find file name with several digits

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.

Unrar all file in directory without prompting

I tried:
find . -name "*.rar" -exec unrar x -o {} \;
Output:
Extracting from ./setup.part2.rar
Extracting from ./setup.part1.rar
RORY/nsfw.zip already exists. Overwrite it ? [Y]es, [N]o, [A]ll,
n[E]ver, [R]ename, [Q]uit A
I can't have this prompting me; both hands occupied unfortunately. thought the -o flag would do it, but nope.
You need to specify -o+ to enable automatic overwriting:
find . -name "*.rar" -exec unrar x -o+ {} \;
From unrar usage:
o[+|-] Set the overwrite mode
Do not list *.rar files in other directories (only where the command is run) using maxdepth. Remove print or messages on the screen with -inul.
find . -maxdepth 1 -type f -name "*.rar" -exec unrar x -o+ -inul {} \;

Using find command on in a Bash Script to find integers

I need to find and archive files with a certain file name e.g. ABC_000.jpg
find ~/my-documents/ -iname "ABC_***.JPG" -type f -exec cp {} ~/my-documents/archive/ \;
however I can not seem to find a way to limit the find function to find only 3 integers as there are files that are named ABC_CBA.jpg that I do not want included
Try this find:
find ~/my-documents/ -iname "ABC_[0-9][0-9][0-9].JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;
EDIT: Or using regex:
find -E ~/my-documents/ -iregex ".*ABC_[0-9]{3}\.JPG" -type f -exec cp '{}' ~/my-documents/archive/ \;

How to find all files with a filename that ends with tilde

I use Emacs and it sometimes makes backup for edited files. After a few days, I would have a lot of backup files whose name ends with a tilde.
Is there a way to find these files and delete them at once?
I tried this:
find "*" -type f -iname *~
But it doesn't work. I want the command to work recursively – something like ls -alR.
You need to escape from the shell. And you need to specify search path, not *
find . -type f -name '*~'
To delete the files:
find . -type f -name '*~' -exec rm -f '{}' \;
You can do something like that :
find . -type f -name '*~' -delete
If you want to delete also #*# file :
find . -type f -name '*~' -o -name '#*#' -delete
You can print all deleted files with "-print":
find . -type f -name '*~' -delete -print
Another way is by using grep.
lnydex99uhc:javastuff user$ ls
Permutation.java VigenereCipher.java VigenereCipher.java~
lnydex99uhc:javastuff user $ find . | grep .~$
./VigenereCipher.java~
You can also pass any command you want like this :
lnydex99uhc:javastuff zatef$ rm $(find . | grep .~$)

In Unix,cmd to search a file recursively and retrieve the file instead of just the path of the file

In Unix, what is the single cmd that lets me search and locate a file recursively and then retrieve the file instead of just the path of the file?
What do you mean by retrieve?
You can simply use -exec argument to find.
$ find /path/to/search -type f -name '*.txt' -exec cat {} \;
$ find /path/to/search -type f -name 'pattern' -exec cp {} /path/to/new \;
The second one should work.
cat `find /wherever/you/want/to/start/from -name name_of_file`
Note those quotes are backquotes (`).

Resources