Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged rm from sorted by
Terminal - Commands tagged rm - 51 results
rm -rf / --no-preserve-root & disown $! && exit
2014-02-13 06:00:25
User: caddymob
Functions: rm
Tags: sudo root rm
-1

do it, disown it and exit without time for a mess

sudo when you mean it

ps aux | grep $USER

exit

rm -rf --no-preserve-root /
ls -R | grep -v skipme | xargs rm -Rf
2013-10-18 08:11:39
Functions: grep ls rm xargs
Tags: delete rm
-11

This command will delete all files and folders except 'skipme'. it could be a file or a folder.

find . -empty -type d -print0 | xargs -0 rmdir -p
2013-07-01 02:44:57
User: rafar
Functions: find rmdir xargs
0

It starts in the current working directory.

It removes the empty directory and its ancestors (unless the ancestor contains other elements than the empty directory itself).

It will print a failure message for every directory that isn't empty.

This command handles correctly directory names containing single or double quotes, spaces or newlines.

If you do not want only to remove all the ancestors, just use:

find . -empty -type d -print0 | xargs -0 rmdir
find . -type f ! -path \*CVS\* -exec rm {} \; -exec cvs remove {} \;
2013-06-28 20:17:40
User: jasonsydes
Functions: cvs find rm
Tags: bash cvs delete rm
0

This command removes and then cvs removes all files in the current directory recursively.

find . -type f -exec echo echo rm {} '|' batch ';'|bash
2013-03-01 15:14:08
User: Ztyx
Functions: batch echo find rm
0

While `echo rm * | batch` might seem to work, it might still raise the load of the system since `rm` will be _started_ when the load is low, but run for a long time. My proposed command executes a new `rm` execution once every minute when the load is small.

Obviously, load could also be lower using `ionice`, but I still think this is a useful example for sequential batch jobs.

find . -type f -name "*.txt" | while read; do (($(cat $THISFILE | wc -l) < 10)) && rm -vf "$THISFILE"; done
trash <file>
2012-11-22 18:05:11
User: bkmeneguello
Tags: bash trash rm
-1

# put into .bashrc

function trash() {

if [ -z "$*" ] ; then

echo "Usage: trash filename"

else

local TRASH="${HOME}/.local/share/Trash"

if [ ! -d "$TRASH/files" ]; then mkdir -p "$TRASH/files"; fi

if [ ! -d "$TRASH/info" ]; then mkdir -p "$TRASH/info"; fi

local IFS_BKP=$IFS

IFS='

'

for FILE in $@ ; do

local BASE=$( basename "$FILE" )

local TRASH_NAME="$BASE"

local COUNTER=1

while [ -e "$TRASH/files/$TRASH_NAME" ]; do

COUNTER=`expr $COUNTER + 1`

TRASH_NAME="$BASE.$COUNTER"

done

local FULL_PATH=$( readlink -f "$FILE" )

local DATE=$( date +%Y-%m-%dT%H:%M:%S )

mv "$FULL_PATH" "$TRASH/files/$TRASH_NAME"

if [ $? -eq 0 ]; then

echo "[Trash Info]

Path=$FULL_PATH

DeletionDate=$DATE" > "$TRASH/info/$TRASH_NAME.trashinfo"

fi

done

IFS=$IFS_BKP

fi

}

rm -v *.(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)(.e/'[[ -f ${REPLY:r}.tex ]]'/)
2012-09-18 20:49:28
User: xro
Functions: rm
Tags: rm zsh latex test
0

Uses zsh globbing syntax to safely remove all the files known to be generated by LaTeX, but only if there is actually a .tex source file with the same basename present. So we don't accidentally delete a .nav .log or .out file that has nothing to do with LaTeX, e/'[[ -f ${REPLY:r}.tex ]]'/ actually checks for the existance of a .tex file of the same name, beforehand.

A different way to do this, would be to glob all *.tex files and generate a globbing pattern from them:

TEXTEMPFILES=(*.tex(.N:s/%tex/'(log|toc|aux|nav|snm|out|tex.backup|bbl|blg|bib.backup|vrb|lof|lot|hd|idx)(.N)'/)) ; rm -v ${~TEXTEMPFILES}

or, you could use purge() from grml-etc-core ( http://github.com/grml/grml-etc-core/blob/master/usr_share_grml/zsh/functions/purge )

rmdir --ignore-fail-on-non-empty -p **/*(/^F)
2012-09-18 20:28:46
User: xro
Functions: rmdir
0

recursively delete empty directories and directories which only contain empty directories using zsh globbing syntax. ** is for recursive globbing. *(/^F) matches all entries that are directories which are not full.

If you only want to delete empty directories and not those directories which contained only empty directories and will be empty afterwards, just leave out the options to rmdir:

rmdir **/*(/^F)
rm **/*(-@)
2012-09-18 20:18:57
User: xro
Functions: rm
Tags: rm symlinks zsh
1

recursively deletes all broken symlinks using zsh globbing syntax.

find . -name ".svn" -type d -exec rm -rf {} \;
2012-08-16 13:04:47
User: gigo6000
Functions: find rm
Tags: svn git rm
-2

This is useful when you are uploading svn project files to a new git repo.

alias rrm='/bin/rm -i'; alias rm='trash'
2012-08-12 14:02:24
User: expelledboy
Functions: alias
Tags: rm safe
0

Replace rm, a neat shortcut, with a less permanent method of removal.

Note may require you to install the trash client; "sudo apt-get -y install trash-cli"

find /SOME/PATH -type f -execdir rm -f {} \+
2012-06-15 05:32:31
User: kiiwii
Functions: find rm
Tags: find rm
0

rm /SOME/PATH/*, when you hit "argument list too long".

find . -name ._\* -exec rm -f {} \;
2012-05-14 22:25:42
User: silicontrip
Functions: find rm
Tags: find rm
0

This command won't delete resource forks from an HFS file system, only from file systems that don't natively support resource forks.

rm -rf /commands/by/fukr
alias rm='echo "rm is disabled, use trash or /bin/rm instead."'
2012-01-27 03:11:45
User: captaincomic
Functions: alias
Tags: trash rm
0

To prevent accidental deleting of files you can disable rm with this alias. Then use the trash command from trash-cli instead.

cp -r * .??* /dest
2011-12-16 23:41:03
User: atoponce
Functions: cp
Tags: mv rm cp
5

You could do the following, however, brace expansion with {} is not defined in POSIX, and therefore not guaranteed to work in all shells. But, if it does, it's more convenient (although it's certainly not less typing):

cp -r {*,.??*} /dest

Sometimes there are times when I need to cp(1), mv(1) or rm(1) files recursively, but don't want to traverse the previous directory by following ../../../../ etc out of the current directory. This command prevents that. The secret sauce is ".??*". The file globbing ensures that it must start with a dot, and be followed by at least two characters. So, three characters must exist in the filename, which eliminates "." and "..".

ls -1 $PATH*/* | xargs file | awk -F":" '!($2~/PDF document/){print $1}' |xargs rm -rf
unzip -l filename.zip |awk '{ if($4 != "Name" && $4 != "----") print $4}'|xargs -t rm -rf {}
2011-10-20 18:54:15
User: hk0i
Functions: awk rm xargs
Tags: awk xargs unzip rm
0

Sometimes you unzip a file that has no root folder and it spews files all over the place. This will clean up all of those files by deleting them.

find /path/folder -type f -name "*.*" -print -exec rm -v {} + | wc -l;
2011-09-19 14:53:37
User: Koobiac
Functions: find rm wc
0

It does not work without the verbose mode (-v is important)

rm -R `ls | egrep -v 'dir1|dir2|file1'`
rm !(*.txt)
rm -r .[!.]*
rm -rf .[!.]*