LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   shell script needed (http://www.linuxquestions.org/questions/linux-general-1/shell-script-needed-606382/)

pirhana 12-12-2007 05:18 PM

shell script needed
 
Hi

I'm fairly new to linux and have a specific requirement. I need a command or script to remove (delete) files with a tilde (~) anywhere in their filename, recursively through all subfolders.

Is this possible?

I'm thinking along the lines of this command, which renames all *.php4 files to *.php in the current and all subfolders (given to me by an ex colleague):
Quote:

for file in *.php4 ; do mv $file `echo $file | sed 's/\(.*\.\)php4/\1php/'` ; done
Could this be modified to look for a "~" anywhere in the filename and if any are found, remove the file?

Even if you can't help, any ideas are welcome. Thanks.

colucix 12-12-2007 05:34 PM

Quote:

Originally Posted by pirhana (Post 2988754)
I'm fairly new to linux and have a specific requirement. I need a command or script to remove (delete) files with a tilde (~) anywhere in their filename, recursively through all subfolders.

Use find and xargs.

rsashok 12-12-2007 05:44 PM

Try this first:

find . -type f -name "*~*" | xargs echo "rm -f "

If it lists all the files with "~" in the name, remove "echo" as in:
find . -type f -name "*~*" | xargs rm -f

I have not try it, but should work

matthewg42 12-12-2007 06:33 PM

Although it does vary from program to program, it is usual that backup files only have the tidle at the end of the file name - not anywhere in the name. I am assuming that with these commands, but you can add another * after the tilde if you like.

Better to use the -print0 option to find and the -0 option to xargs to make sure you don't get problems with file names which contain spaces, tabs, or new line characters.
Code:

find /base/dir/of/search -type f -name "*~" -print0 |xargs -n 1 -0 echo will remove
If that command shows only files you are sure you want to remove, you can change the xargs part to do the actual removal:
Code:

find /base/dir/of/search -type f -name "*~" -print0 |xargs -0 rm -f

pirhana 12-13-2007 04:15 AM

Thank you very much.

I have tried the above examples and can confirm they work perfectly.

This removed all files containing a ~ anywhere in the filename, in the current folder and all subfolders:
Quote:

find . -type f -name "*~*" | xargs rm -f
I can run this one as a shell script as it allows me to enter the folder path:
Quote:

find /base/dir/of/search -type f -name "*~" -print0 |xargs -0 rm -f
Combining the two I have:
Quote:

find /test/test_subfolder -type f -name "*~*" -print0 |xargs -0 rm -f
This is for cleaning up after a backup script. The script appends the date/time to the existing file, so backing up test.php gives:
-rw-r--r-- 1 root root 9 Dec 13 09:59 test.php
-rw-r--r-- 1 root root 4 Dec 13 09:50 test.php~2007-12-13_09:59

which leaves me with a valuable folder of all historical files in case of errors.


All times are GMT -5. The time now is 03:37 PM.