![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
How do find file in directory or subdirectory ????
|
|
#3
|
|||
|
|||
|
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 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$ /Lakris |
|
#4
|
|||
|
|||
|
Code:
find <dir> -name <shell-regex-pattern> -exec <command> {} \;
find some/dir -name "*.jpg" -exec ls -l {} \;
is this what you want? Last edited by m9dhatter; 01-23-2008 at 07:57 PM. |
|
#5
|
|||
|
|||
|
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...
/Lakris |
|
#6
|
|||
|
|||
|
yep. if its a list, that would better suit his query.
|
|
#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"); |
|||
| Google The UNIX and Linux Forums |