Regarding real example of user of semicolon(;) and + in find/exec command.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Regarding real example of user of semicolon(;) and + in find/exec command.
# 1  
Old 07-13-2017
Regarding real example of user of semicolon(;) and + in find/exec command.

Hello All,

Was recently working on an requirement where we have to search files more than a specific number, following is the example on same.
Let's say file names are test_40000.txt,test_40001.txt and so on till test_99999.txt.

Now requirement was to search from find command only those files whose integer value is greater than 40000 in it.

So I have created these files for testing purposes(with ZERO size also). Now as we know for fast processing of find along with exec we could use {} \+ at last of it because it processes all the files in single shot rather than 1 by 1 and will be faster than using {} \; at end of find,exec command. In forums I have seen people asking about it how {} \+ will be faster than {} \;, then here is an example for it too.
Let's say I created following files.
Code:
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:31 test_40003.txt
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:32 test_40004.txt
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:32 test_40005.txt
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:32 test_40006.txt
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:32 test_40007.txt
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:32 test_40008.txt
-rw-rw-r-- 1 singh_test singh_test    0 Jul 12 14:32 test_40009.txt
-rw-rw-r-- 1 singh_test singh_test   78 Jul 12 15:21 test_40000.txt
-rw-rw-r-- 1 singh_test singh_test   78 Jul 12 15:21 test_40001.txt
-rw-rw-r-- 1 singh_test singh_test   78 Jul 12 15:21 test_40002.txt

So now I had prepared the following find command to satisfy the condition of getting specific files.
Code:
find -type f  -exec awk --re-interval  'FILENAME ~ /test_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++{;print FILENAME} \
END{if(FILENAME ~ /test_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++){print FILENAME}}' {} \+

So output will be as follows for above command.
Code:
./test_40002.txt
./test_40000.txt
./test_40001.txt

Now following are the points on above output.

I- If we see it carefully there is NO 0 size file in output.
II- IMHO it is because of {} \+ it will collect all the file names first(all the files) then when it comes to -exec it will pass all the files to awk in single shot, let's say in following style(as an example).
Code:
awk --re-interval  'FILENAME ~ /test_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++{;print FILENAME} END{if(FILENAME ~ /test_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++){print FILENAME}}' test_40000.txt test_40001.txt .......

III- So point to be noted here, how could we confirm that it is executing files I above fashion because, I had created NULL size files in system which are coming into the regex pattern and should be picked successfully.
IV- But they are NOT getting picked up why?
V- Because awk will NOT be able to process any empty size file, only thing in an awk program we could do is could work on END section which will obviously be executed an the end of all files are done with execution/processing by awk.
VI- So that only it is NOT picking those NULL size files, AND
VII- Hence proved that using \+ will be collecting all the information from find(command's whatever conditions) and then will execute them in a single shot.

Now on the other hand when I run following command using {} \; it will collect the files one by one and execute the exec function one by one for them so then NULL size files will be picked SUCCESSFULLY.
Following is the example on same.
Code:
find -type f  -exec awk --re-interval  'FILENAME ~ /test_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++{;print FILENAME} \
END{if(FILENAME ~ /test_[4-9][0-9]{4,}.txt/ && !a[FILENAME]++){print FILENAME}}' {} \;

Output will be as follows for above command.
Code:
./test_40008.txt
./test_40005.txt
./test_40007.txt
./test_40006.txt
./test_40002.txt
./test_40004.txt
./test_40000.txt
./test_40001.txt
./test_40009.txt
./test_40003.txt

I had this learning, so ASAP I thought of sharing with al here so all could be benefited by this. Please feel free to correct me or any feed back on same.

Thanks,
R. Singh

Last edited by RavinderSingh13; 07-13-2017 at 08:37 AM..
# 2  
Old 07-14-2017
awk or sed are text file processors; they loop over the lines in a file. To only print FILENAME looks like a misuse of awk.
You would need to loop over the arguments in the BEGIN section.
Code:
find . -type f  -exec awk 'BEGIN { for (i=1; i<ARGC; i++) if (ARGV[i] ~ /test_[4-9][0-9]{4}.txt/) print ARGV[i] }' {} +

Still this looks like the wrong tool for the task.
If there is a simple search pattern for the filenames one can use a -name glob:
Code:
find . -type f -name "test_[4-9][0-9][0-9][0-9][0-9].txt"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Linux find command seems to not transmit all the result to the '-exec command'

Hello. From a script, a command for a test is use : find /home/user_install -maxdepth 1 -type f -newer /tmp/000_skel_file_deb ! -newer /tmp/000_skel_file_end -name '.bashrc' -o -name '.profile' -o -name '.gtkrc-2.0' -o -name '.i18n' -o -name '.inputrc' Tha command... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Find and move command with exec

Hi all, I am trying to find files newer than a given file and them mv them to a new location. So I far I have: find . ! -newer <file_name> -exec ls -l {} \; and find . ! -newer <file_name> -exec mv /TEMP_LOCATION {} \; find is not liking this. Anyone know how to modify the last... (2 Replies)
Discussion started by: jonnyd
2 Replies

3. UNIX for Dummies Questions & Answers

What does the '\' in find -exec command

Hi, I have two scripts that remove files. One works fine and is coded find -name "syst*" -mtime +1 -exec rm {} \; The other is almost the same - only thing missing is the '\'. On that script though I keep getting: rm syst1202.file ? etc Does the \ make that difference or is it a... (3 Replies)
Discussion started by: Grueben
3 Replies

4. Shell Programming and Scripting

find command with -exec

Hi all, Please could someone help with the following command requirement. I basically need to find files NEWER than a given file and order the result on time. My attempt so far is as follows: find . -newer <file_name> -exec ls -lrt {} ;\ But I dont seem to get the right result... (12 Replies)
Discussion started by: jonnyd
12 Replies

5. UNIX for Dummies Questions & Answers

df -h command, can't seem to find real location

Hi, I need your help. I am at a new place, just trying to understand what's going on here. When I do df -h, I see many mounts. But most of them are automount, how do I find the real location? netappt1:/vol/homet2/sthan 1.7T 1.2T 527G 69% /home/sthan... (4 Replies)
Discussion started by: samnyc
4 Replies

6. Shell Programming and Scripting

How to get the exit code of -exec in the find command

Hi I have a little problem with the find command in a script that I'm writing. The script should check if there are some files younger than 100 seconds and then syncronise them with rsync. My find command: find -type f -cmin -100 -exec rsync -a --delete directory1/ directory2/ When I... (8 Replies)
Discussion started by: oku
8 Replies

7. Shell Programming and Scripting

find command with -exec

Hi People, I have a directory full of compressed files (.Z extention) In many of these files there is a string pattern (3800078163033) I want to find all file names which contain this string in their text. Regards, Abhishek (2 Replies)
Discussion started by: max29583
2 Replies

8. UNIX for Advanced & Expert Users

Find command with prune and exec

Hi, I'm using the following command to get a list of files on the system. find /releases -type f -exec ls -l > /home/sebarry/list.txt '{}' \; however, its searching a directory I don't want it to search so I know I have to use prune but I don't seem to be able to get prune and exec to work... (1 Reply)
Discussion started by: Sebarry
1 Replies

9. UNIX for Dummies Questions & Answers

find command exec error

Hi All, i am writing a shell script in korn shell which deletes all the files in a directory once in every 10DAYS. the directory has different format files. the script has something like this; cd /home/data/pavi echo "Please Enter the Number of Days to search for" read DAYS... (2 Replies)
Discussion started by: pavan_test
2 Replies
Login or Register to Ask a Question