LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-12-2007, 05:18 PM   #1
pirhana
LQ Newbie
 
Registered: Nov 2007
Location: UK
Distribution: CentOS 5
Posts: 20

Rep: Reputation: 0
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.
 
Old 12-12-2007, 05:34 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,488

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by pirhana View Post
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.
 
Old 12-12-2007, 05:44 PM   #3
rsashok
Member
 
Registered: Nov 2006
Location: USA, CA
Distribution: RedHat, Debian
Posts: 202

Rep: Reputation: 31
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
 
Old 12-12-2007, 06:33 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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

Last edited by matthewg42; 12-12-2007 at 06:34 PM.
 
Old 12-13-2007, 04:15 AM   #5
pirhana
LQ Newbie
 
Registered: Nov 2007
Location: UK
Distribution: CentOS 5
Posts: 20

Original Poster
Rep: Reputation: 0
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.
 
  


Reply

Tags
backup, delete, find, rm, xargs


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help needed on Mass user creation using shell script EsAsher Linux - General 3 06-29-2007 11:26 PM
shell backup script help needed Quakeboy02 Linux - Software 4 04-26-2007 05:20 PM
Shell Script needed urgently!!!! krudupa9 Linux - General 1 06-27-2006 12:51 PM
simple shell script help needed vbsaltydog Linux - Networking 10 05-02-2006 01:47 AM
Help needed learning to write a shell script for logons nistur Linux - Networking 8 10-02-2004 02:34 AM


All times are GMT -5. The time now is 11:20 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration