Puzzled by Find

 
Thread Tools Search this Thread
Operating Systems OS X (Apple) Puzzled by Find
# 1  
Old 12-22-2011
Question Puzzled by Find

I'm new to playing with the command line on OS X and am puzzled by the response I am getting from the find command. I have a file structure similar to the following

/Volumes/
../Drobo/
../../Pictures/
../../../Image 1/
../../../../Image 1.jpg
../../../../Previews/
../../../../../Image 1.jpg
../../../Image 2/
../../../../Image 2.jpg
../../../../Previews/
../../../../../Image 2.jpg

and so on for hundreds or in some cases thousands of directories at the image level. I am trying to pull all of the jpg files that are under the main group to the Pictures/ level without having them overwritten by any of the same name files from the Previews/ directories. To do this, I've set up the following two commands:

find "$PWD" -type d -iname "Previews" -exec rm -R {} \;
find "$PWD" -type f -iname "*.jpg" -exec mv {} . \;

which I execute from the Pictures/ directory level. It works, but a few things puzzle me.

1. This only works if I use the $PWD variable. If I use a . there, the rm command fails.

2. I have to use the -exec rm to succeed. If I try using the -delete flag on the first command, it fails.

3. Even though the Previews/ directories are being deleted, I receive the following error message on the terminal:

find: /Volumes/Drobo/Pictures/Image 1/Previews: No such file or directory

Anyway, I'm looking for any hints as to answers to my three areas of confusion above, and/or an easier way to accomplish the task. As I say, it's all working, so I'm just trying to understand what i'm seeing.

Thanks, this looks like a wonderful board.
# 2  
Old 12-23-2011
1) Because in your example you are trying to delete the jpg files "recursively" using rm command & "." refers to current directory there is no way for "rm" to recursively delete the files you want if you use "." because it will search only current directory for "preview" & it won't search the subdirectories for "preview".
2) Try using "." with "-delete" flag. Let me know if it works.
3) The error is because after recursively deleting the files & directories "find" is not able to traverse back.
I hope this answer your queries.
# 3  
Old 12-23-2011
1. Remove the double quotes around Previews. This works:
Code:
find . -type d -iname Previews -exec rm -R {} \;

2. Find's delete option will not delete a directory with files in it.

3. Suppress the error message by changing the rm options then send any find error messages to /dev/null:
Code:
find . -type d -iname Previews -exec rm -rf {} \; 2>/dev/null


Last edited by xbin; 12-23-2011 at 12:40 PM.. Reason: fix last command
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Serioulsy puzzled here.

Facebook had a mathematics problem which was as thus:- 6/2(1+2) = ? Answer is 9. My ancient Casio FX 730P mini computer written exactly as that gives 'error' only. Now take a look at shell versions, and a python version:- Last login: Wed Sep 14 18:04:04 on ttys000 AMIGA:barrywalker~>... (6 Replies)
Discussion started by: wisecracker
6 Replies

2. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

3. Shell Programming and Scripting

How to use grep & find command to find references to a particular file

Hi all , I'm new to unix I have a checked project , there exists a file called xxx.config . now my task is to find all the files in the checked out project which references to this xxx.config file. how do i use grep or find command . (2 Replies)
Discussion started by: Gangam
2 Replies

4. Shell Programming and Scripting

Puzzled with hexdump, hd and ln

How to create a symbolic link to a command with certain argument? When I man hexdump, it is said in the man page that "-C Canonical hex+ASCII display...Calling the command hd implies this option". Actually it is. hd equals to hexdump -C. And then I examined the ln command but find it is a... (5 Replies)
Discussion started by: vistastar
5 Replies

5. Solaris

Puzzled over over the relationship between the partition and geometry of hard disk.

Not sure why solaris couldn't detect the geometry of a hard disk which has a working OS of winxp pro. Is it due to the different OS that the partition information is stored in different location? When I type '"format" it is shown as below, c3d1 < drive type unknown>... (5 Replies)
Discussion started by: just.srad
5 Replies

6. Solaris

puzzled with VxVM and iostat..

Hi all, One disk on my root disk group failed in Veritas Volume manager. I replaced it with new one, initialized it and placed it with removed one. it Synchronized plexes and everything is fine. this node was second standby node of Sun cluster. yesterday I had failure on active node with boot... (1 Reply)
Discussion started by: samar
1 Replies

7. Programming

C++ Puzzled !!

#include <iostream.h> class A { public: void f(void) { cout << "hello world \n" ; } }; void main() { A *a; a = 0 ; a->f(); // OOPs...Am I mad? What am I going to do ? } (1 Reply)
Discussion started by: RipClaw
1 Replies

8. Shell Programming and Scripting

command find returned bash: /usr/bin/find: Argument list too long

Hello, I create a file touch 1201093003 fichcomp and inside a repertory (which hava a lot of files) I want to list all files created before this file : find *.* \! -maxdepth 1 - newer fichcomp but this command returned bash: /usr/bin/find: Argument list too long but i make a filter all... (1 Reply)
Discussion started by: yacsil
1 Replies

9. Programming

Puzzled with user ID.

I hava been reading AUPE these days. I really am puzzled with the presentation of real user(group) ID, effective user(group) ID. How do they effect on the execution of process? What's the relationship between them? Appreciate your help. (4 Replies)
Discussion started by: lethefe
4 Replies
Login or Register to Ask a Question