//Tips tagged rm
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
To delete a file who's file name is a pain to define (eg. ^H^H^H) find it's inode number with the command "ls -il". Use the line below to find and delete the file.
find . -inum 12345 | xargs rm

View Comments »

It's easy to remove (or copy, move etc.) all files that match a given criteria, but harder to move all but ones excluded by a criteria.

To do this we can combine grep's -v option with Unix command substitution:

$ ls
1.txt   2.txt   3.txt   4.txt
$ rm `ls | grep -v 4\.txt`
$ ls

View Comments »

This is a script that I recently wrote to find the logs of my IDP that were over 90 days old and delete them.

The script is pretty much self explanatory.

## Lets find the log directories that are over 90 Days old.
find /var/netscreen/DevSvr/logs -mtime +90 -type d -prune -print > /root/scripts/files-deleted

## Now let's log the directories we are going to delete.
touch /root/scripts/deletelog

## We want to know when the files were deleted so we will append that to the log file.
date >> /root/scripts/deletelog

## Now we go through and delete the directories that are over 90 days old
## and log the directories that we delete to the log file.
for FILE in `cat /root/scripts/files-deleted`
echo Deleting $FILE ... >> /root/scripts/deletelog
rm -Rf $FILE

## Finally we will email a copy of the log file to the admin for his/(or her) review.
cat /root/scripts/deletelog | mail -s "90 Day IDP log cleanup" youremailaddress@domian.com

View Comments »

The script below will find duplicate files (files with the same md5sum) in a specified directory and output a new shell script containing commented-out rm statements for deleting them. You can then edit this output to decide which to keep.

echo "#! /bin/sh" > $OUTF;
find "$@" -type f -print0 |
  xargs -0 -n1 md5sum |
    sort --key=1,32 | uniq -w 32 -d --all-repeated=separate |
    sed -r 's/^[0-9a-f]*( )*//;s/([^a-zA-Z0-9./_-])/\\\1/g;s/(.+)/#rm \1/' >> $OUTF;
chmod a+x $OUTF; ls -l $OUTF

View Comments »

Use find by time to delete files more than x days old. For example the command below will delete files more than one day old:

find . -mtime +1 -exec rm {} \;

View Comments »

ls | xargs rm

Sometime there are so many files in a directory than the rm command doesn't work
[root@server logs]# rm *
bash: /bin/rm: Argument list too long

On this case the best option is to use ls in conjuntion with xargs
[root@server logs]# ls | xargs rm

View Comments »

.DS_Store files are a hidden file created by OS X to store custom attributes of a folder such as the position of icons or the choice of a background image. By default, they are created in every folder accessed, even folders on remote systems (for example, folders shared over an SMB or AFP connection). This can soon leave your shared folders littered with these files.

The command below will remove all DS_Store files from the current directory and any sub directories.

find -type f -name .DS_Store -exec rm {} \;

View Comments »

CASE: You've relocated Subversion and Trac repositories to another machine/directories. You don't want to edit n+1 trac.ini files.

You have for example:



Change ALL trac.ini repository_dir settings:


#!/bin/bash TRACSPATH=$1 REPOPATH=$2 for i in $( find $TRACSPATH -maxdepth 1 -type d | grep -v "^$TRACSPATH\$" ); do BN=`basename $i` INIPATH=$i/conf/trac.ini TMP=$i/conf/trac.ini-temp # Replace repository_dir cat $INIPATH | perl -pe "s@repository_dir = .*@repository_dir = $REPOPATH/$BN@i" > $TMP mv $INIPATH $INIPATH-old && mv $TMP $INIPATH && rm $INIPATH-old done

./trac-mass-repodir-edit.sh /repos/trac-myprojects /repos/svn-myprojects

Then you want to resync and upgrade all existing Tracs:



for i in $( find $TRACSPATH -maxdepth 1 -type d | grep -v "^$TRACSPATH\$" ); do
  BN=`basename $i`
  # SVN directory exists
  if [ -d $REPOPATH/$BN ]; then
    echo "Processinc Trac: $BN.."
    trac-admin $i resync
    trac-admin $i upgrade

./trac-mass-upgrade.sh /repos/trac-myprojects /repos/svn-myprojects

Now you have everything in order.

View Comments »

Home Latest Browse Top 25 Random Hall Of Fame Contact Submit