find -exec directories with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find -exec directories with spaces
# 15  
Old 03-12-2010
The -depth parameter to "find" should start from the deepest level and work up.
I can see potential issues with "rm -rf" on a directory name because it deletes that directory too.
Personally I have not issued "rm -rf" in years. There is always a safer and more predictable method where you first produce a list to review.


alister's explanation and solution is very good.

Last edited by methyl; 03-12-2010 at 07:00 PM..
# 16  
Old 03-12-2010
Quote:
Originally Posted by markdjones82
The command with '{}' did not work and I am running it from the BASH command line and in a crontab entry.

drew k's command did work however. Thanks drew!
Just curious: does the suggested -exec '{}' for work when you run it directly? ie, not from a crontab entry?
# 17  
Old 03-12-2010
Me too. The issue with "find" and {} or '{}' regarding arguments containing space characters has been evident for many years. Having examined the source code of a current unix "find" there is no reason to quote {}. However in my experence it is sometimes necessary to quote '{}'. This board has experienced posters who could resolve this issue. With current shells it has to be something to do with context.
# 18  
Old 03-13-2010
Quote:
Originally Posted by alister
[...] A better option would be -prune.

Code:
find /home/chroot/marketing/ -mindepth 3 -type d -prune -exec rm -rf {} \;

Definitely.
As far as efficiency is concerned,
one could reduce further the fork/exec storm:

- use + instead of \; to make it work like xargs (n at a time):

Code:
... -exec rm -r {} +

(I believe that -f is not needed in a non interactive sessions, i.e. cron)

or, if the + syntax is not supported:

- pipe the output to xargs, like already suggested

---------- Post updated at 09:03 AM ---------- Previous update was at 08:56 AM ----------

Quote:
Originally Posted by methyl
[...]
After a debate a while back with jlliagre on the subject of quoting '{}' please do post whether quoting '{}' works in this circumstance. It would be my solution but it would also mean that this version of "find" is considered abnormal or that the shell is misbehaving with the braces {}.
[...[
Yep,
I remember that discussion very well,
that's why I asked for details Smilie

And that's why I suggested the depth option, to see if the filename really matters.
# 19  
Old 03-16-2010
Quote:
Originally Posted by drewk
Just curious: does the suggested -exec '{}' for work when you run it directly? ie, not from a crontab entry?
Nope, none of the '{}' options or using the -depth worked, but the xargs did work.
# 20  
Old 03-16-2010
Quote:
Originally Posted by methyl
The issue with "find" and {} or '{}' regarding arguments containing space characters has been evident for many years.
or has been a urban legend for many years, depending on who you ask ... Smilie
Quote:
Having examined the source code of a current unix "find" there is no reason to quote {}.
You are still missing these quotes are stripped off by the shell before the braces are passed to the find command and so there is no much point in expecting them to have an effect on the find command itself. The only reason to quote them would be to use a shell that has a special interpretation of these curly braces, but there is no such beast around.
Quote:
However in my experence it is sometimes necessary to quote '{}'. This board has experienced posters who could resolve this issue.
There is no such evidence I'm aware of.

Anyway, back to the open poster issue, it would be interesting to get a reproducible use case to figure out why xargs work where find alone doesn't.

I'm especially interested seeing what display these three commands:
Code:
 find /path -mindepth 3 -type d -exec /bin/rm -rf {} \;

Code:
 find /path -mindepth 3 -type d -exec rm -rf {} +

and
Code:
 find /path -mindepth 3 -type d -exec echo "[" {} "}" \;

# 21  
Old 03-16-2010
Quote:
Originally Posted by jlliagre
[...]
The only reason to quote them would be to use a shell that has a special interpretation of these curly braces, but there is no such beast around.
This is not true:

Code:
; find /tmp -type d -exec ls -ld {} +
syntax error
; find /tmp -type d -exec ls -ld '{}' +
drwxrwxrwt+ 1 sysadmin root 0 2010-03-16 17:01 /tmp
; echo $version
1.7.1 $Release: @(#)rc 1.7.1 2003-07-17 $

I think you'll get the same or similar error with es.

Quote:
There is no such evidence I'm aware of.
[...]
As already demonstrated in the older thread:

Code:
% touch a\ b
% find . -type f -ls
4222124650680077    0 -rw-r--r--   1 sysadmin None            0 Mar 16 21:16 ./a\ b
% find . -type f -exec sh -c 'f={}' - \;
-: b: command not found
% find . -type f -exec sh -c 'f="{}"' - \;
%


Last edited by radoulov; 03-16-2010 at 05:32 PM.. Reason: Adjusted the quoted part.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directories having spaces

Hi I have a list file with complete path of files and there are directories with spaces. Now when i read the list file, read the line and read the file it says no file found because of the space. How can I get through this issues for file in $(<list_file.txt) do first_line=`head -1... (8 Replies)
Discussion started by: lijjumathew
8 Replies

2. Shell Programming and Scripting

Directories with spaces

My code path=`find /root/folder/ -maxdepth 100 -type d | sort --random-sort | head -1` if this returns a directory with spaces in for instance: /root/folder/this is a folder is there a way to replace the value of path with the same value but with the spaces replaced so its... (2 Replies)
Discussion started by: digitalviking
2 Replies

3. Shell Programming and Scripting

find: missing argument to `-exec' while redirecting using find in perl

Hi Friends, Please help me to sort out this problem, I am running this in centos o/s and whenever I run this script I am getting "find: missing argument to `-exec' " but when I run the same code in the command line I didn't find any problem. I am using perl script to run this ... (2 Replies)
Discussion started by: ramkumarselvam
2 Replies

4. UNIX for Dummies Questions & Answers

Using grep command to find the pattern of text in all directories and sub-directories.

Hi all, Using grep command, i want to find the pattern of text in all directories and sub-directories. e.g: if i want to search for a pattern named "parmeter", i used the command grep -i "param" ../* is this correct? (1 Reply)
Discussion started by: vinothrajan55
1 Replies

5. UNIX for Dummies Questions & Answers

Find Exec

Hello All, Is there a way to make exec do a couple of operations on a single input from find? For example, find . -type d -exec ls -l "{}" ";" I would like to give the result of each "ls -l" in the above to a wc. Is that possible? I want to ls -l | wc -l inside... (1 Reply)
Discussion started by: prasanna1157
1 Replies

6. Shell Programming and Scripting

How to find 777 permisson is there or not for Directories and sub-directories

Hi All, I am Oracle Apps Tech guy, I have a requirement to find 777 permission is there or not for all Folders and Sub-folders Under APPL_TOP (Folder/directory) with below conditions i) the directory names should start with xx..... (like xxau,xxcfi,xxcca...etc) and exclude the directory... (11 Replies)
Discussion started by: gagan4599
11 Replies

7. Shell Programming and Scripting

Script to find folders with spaces and end of files and directories

Hi I need a script that can search through a set of directories and can locate any file or directory that has a space at the end Filename(space) Foldername(space) I then need to remove that space within the script Hope someone can help thanks in advance Treds (8 Replies)
Discussion started by: treds
8 Replies

8. Shell Programming and Scripting

find & dirname:problems with white spaces in Directories

Hi all, my problem: (little extract from my bash-script) I want to move each file (.mov) from one directory (and many Subdirectories) to another directory (only one); after moving i want to create hardlinks to the old directories. Thatīs no problem, but now: source-directories... (4 Replies)
Discussion started by: tubian
4 Replies

9. Shell Programming and Scripting

Directories with spaces in name - looking for help with questions in that issue

I need to work with 'nice' directory names which have some spaces in name, and that brings some questions and not-understanding. Would some expert help me out how to deal with that?! My task is to document some processing, running from some script. I do need to have spaces in directories... (2 Replies)
Discussion started by: alex_5161
2 Replies

10. UNIX for Advanced & Expert Users

find and exec

Hi, Happy new year. Would you be so kind to explain me what does this instruction : find /rep/app -type l -exec ls -l {} \;> allink.lst Many thanks. (2 Replies)
Discussion started by: big123456
2 Replies
Login or Register to Ask a Question