The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Create Year directory, date subdirectory and archive the file madhunk UNIX for Dummies Questions & Answers 2 12-13-2007 08:25 AM
Find files including subdirectory and Delete thepurple Shell Programming and Scripting 1 10-04-2007 03:57 AM
find files and copy into a directory balireddy_77 Shell Programming and Scripting 4 04-27-2007 12:38 AM
How to calculate file's size in directory and subdirectory KLL Shell Programming and Scripting 4 10-16-2003 04:02 AM
How can I copy files and subdirectory? odogbolu98 UNIX for Dummies Questions & Answers 3 02-15-2002 12:14 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 10
Find files in directory and its subdirectory

I am writing a script which reads a file line by line and then assigns it to a variable like this 1090373422_4028715212.jpg. I have images with file name of this format in some other directory. In my script I want to assign variable with this file name and then find this filename in some other directory and delete it.

Something like: find some/path filename rm filename

I know it can be done with find command but I don't know the correct syntax.

I would really appreciate if someone can help me with this.

Thanks.
Reply With Quote
Forum Sponsor
  #2  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 10
How do find file in directory or subdirectory ????
Reply With Quote
  #3  
Old 01-23-2008
Registered User
 

Join Date: Oct 2007
Posts: 171
Quote:
Originally Posted by jyotib View Post
How do find file in directory or subdirectory ????
Maybe You could try something like this. I am sure there are more efficient ways to do it, especially when there are lots of files and deep structures. But it can give You an idea of how it can be done. This works in Bash. Probably in most other shells as well.

Let's say You have a file with a list of names:
Code:
lakris@Lakrix:~/Projects/scripts$ cat listofnames.txt
resumed-transactions.txt
session.properties
With the command line below You let the operation "while read" (the < listofnames.txt at the end means that that's where the input comes from) take each line, and use it as an argument to the program find.

When the program find finds it in or below ~(the ~ means Your home directory, replace it with something else, /share/pics or whatever), You use that result and pass it as an argument to the program xargs, which then performs an operation on it.

In this case I use ls (list or dir) since it is non-destructive, just to try it out and it will give me a confirmation of which files will be affected. I can replace it with rm when I think I am certain that it will work.

So:

Code:
lakris@Lakrix:~/Projects/scripts$ while read filename;do find ~ -type f 
-name "$filename" -print0 |xargs ls;done < listofnames.txt
/home/lakris/bin/resumed-transactions.txt
/home/lakris/.anjuta/session.properties
lakris@Lakrix:~/Projects/scripts$
Could that work for You?

/Lakris
Reply With Quote
  #4  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Location: Singapore
Posts: 4
Code:
find <dir> -name <shell-regex-pattern> -exec <command> {} \;
find some/dir -name "*.jpg" -exec ls -l {} \;
each result of the find would end up in "{}". so "ls -l" would be executed for each.
is this what you want?

Last edited by m9dhatter; 01-23-2008 at 07:57 PM.
Reply With Quote
  #5  
Old 01-23-2008
Registered User
 

Join Date: Oct 2007
Posts: 171
Well, if he hasn't fallen asleep... or cursed his lack of taking hourly backups...

-exec would be better than how I used xargs in my example. I was thinking of the possibility of long "lists" in the result but it wouldn't apply to how I wrote it. But I think the OP wants to treat a list of of items given, so maybe:

Code:
while read filename;do find ~ -type f -name "$filename" -print0 -exec ls {} \; ;done < listofnames.txt
and when You are sure, replace ls with rm...
would be better? Using rm -rf would delete all files (and directories) matching the pattern given in the constant string (DON'T RUN THAT COMMAND!) and I don't think that is what he wanted.

/Lakris
Reply With Quote
  #6  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Location: Singapore
Posts: 4
yep. if its a list, that would better suit his query. I've also edited the previous post because I noticed it was kinda dangerous for beginners... sorry about that.
Reply With Quote
  #7  
Old 01-23-2008
Registered User
 

Join Date: Jan 2008
Posts: 7
When doing bulk removes, I do it in stages: generate the list, examine, then do the remove. Then I can check that I'm aiming for the correct foot One Last Time(tm) before shooting.

In the case above where the user wants to transform the file names before removing, sed is your friend. Using the ':' character instead of the traditional '/' for the 's' command allows me to avoid tiresome escaping in the sed command.

Code:
find /path -name '*yack*' -exec echo rm {} \; >/tmp/flist
sed 's:/some/path:/another/dir:' </tmp/flist >/tmp/doit.sh
vim /tmp/doit.sh       # aim carefully
bash -x /tmp/doit.sh   # ka-blam
--
Qman
printf("Hello unix.com forums (first post!)\n");
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:30 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0