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 delete from sorted by
Terminal - Commands tagged delete - 23 results
find /path/to/directory -not \( -name .snapshot -prune \) -type f -mtime +365
2013-12-11 14:51:53
User: cuberri
Functions: find
1

Useful when you want to cron a daily deletion task in order to keep files not older than one year. The command excludes .snapshot directory to prevent backup deletion.

One can append -delete to this command to delete the files :

find /path/to/directory -not \( -name .snapshot -prune \) -type f -mtime +365 -delete
redis-cli KEYS "user*" | xargs redis-cli DEL
2013-10-31 04:10:29
User: vp_arth
Functions: xargs
0

If you want to do this in the some one db, use

redis-cli -n5 KEYS "user*" | xargs redis-cli -n5 DEL
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 . -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 . -path ".*/cur/*" -type f ! -newermt "1 week ago" -delete
2012-03-05 15:48:21
User: evolix
Functions: find
0

This can be used to delete or archive old mails. In fact, for archiving its a bit different, you need to archive mails with any tools (e.g archivemail), and then deleting (if you want!).

Here we use -path ".*/cur/*" to avoid files limit in bash globbing and to search in any inbox (e.g .mymail .spam .whatever).

! -newermt "1 week ago" can be read: All files which is older than "1 week ago", adapt it in consequence.

ls -1 $PATH*/* | xargs file | awk -F":" '!($2~/PDF document/){print $1}' |xargs rm -rf
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)

perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
2011-05-23 08:45:34
User: igorfu
Functions: perl
Tags: perl delete
1

Recursively delete empty directories. Use with care.

svn status | grep '^?' | sed -e 's/^? */svn add "/g' -e 's/$/"/g'|sh ; svn status | grep '^!' | sed -e 's/^! */svn delete "/g' -e 's/$/"/g'|sh
find . -type f ! -name "*.foo" -name "*.bar" -delete
2010-10-07 20:17:38
User: sh1mmer
Functions: find
0

This command is recursive and will delete in all directories in ".". It will find and delete all files not specified with ! -name "pattern". In this case it's file extensions. -type f means it will only find files and not directories. Finally the -delete flag ask find to delete what it matches. You can test the command by running it first without delete and it will list the files it will delete when you run it.

find $HOME -name '*.sol' -exec rm {} \;
2010-08-27 06:38:16
User: Tungmar
Functions: find rm
2

Maybe you want first check which files will be deleted:

find $HOME -name '*.sol' -exec echo rm {} \;
grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt
2010-08-19 20:07:42
User: olalonde
Functions: grep
22

grep searches through a file and prints out all the lines that match some pattern. Here, the pattern is some string that is known to be in the deleted file. The more specific this string can be, the better. The file being searched by grep (/dev/sda1) is the partition of the hard drive the deleted file used to reside in. The ?-a? flag tells grep to treat the hard drive partition, which is actually a binary file, as text. Since recovering the entire file would be nice instead of just the lines that are already known, context control is used. The flags ?-B 25 -A 100? tell grep to print out 25 lines before a match and 100 lines after a match. Be conservative with estimates on these numbers to ensure the entire file is included (when in doubt, guess bigger numbers). Excess data is easy to trim out of results, but if you find yourself with a truncated or incomplete file, you need to do this all over again. Finally, the ?> results.txt? instructs the computer to store the output of grep in a file called results.txt.

Source: http://spin.atomicobject.com/2010/08/18/undelete?utm_source=y-combinator&utm_medium=social-media&utm_campaign=technical

curl --cookie name=<cookie_value> --data-urlencode name=my_post_key=<post_key>\&delete=1\&submit=Delete+Now\&action=deletepost\&pid=$c --user-agent Firefox\ 3.5 --url http://url/editpost.php?my_post_key=<post_key>\&delete=1\&submit=Delete+Now\&action=dele
2010-07-14 01:50:48
User: mrlockfs
0

As a user, deletes all your posts from a MyBB board (provided you have the search page listings of all your posts saved into the same directory this command is run from). Full command:

for i in *; do cat $i | grep pid | sed -e 's/;/\ /g' -e 's/#/\ /g' -e 's/pid=/\ /g' | awk -F ' ' '{print $2}' >> posts.txt; done; for c in `cat posts.txt`; do curl --cookie name= --data-urlencode name=my_post_key=\&delete=1\&submit=Delete+Now\&action=deletepost\&pid=$c --user-agent Firefox\ 3.5 --url http://url/editpost.php?my_post_key=\&delete=1\&submit=Delete+Now\&action=deletepost\&pid=$c; sleep 2s; done; echo

for dir in $(find -type d ! -name CVS); do for file in $(find $dir -maxdepth 1 -type f); do rm $file; cvs delete $file; done; done
2010-04-27 16:03:33
User: ubersoldat
Functions: cvs dir file find rm
Tags: bash cvs delete rm
1

This will search all directories and ignore the CVS ones. Then it will search all files in the resulting directories and act on them.

find . -mtime +10 -delete
2010-04-12 15:05:17
User: rexington
Functions: find
2

This will find all files under the path "." which are older than 10 days, and delete them. If you wish to use the "rm" command instead, replace "-delete" with "-exec rm [options] {} \;"

shred -u -z -n 17 rubricasegreta.txt
2010-01-31 15:24:54
User: 0disse0
Functions: shred
6

Instead, install apt-get install secure-delete and you can use:

-- srm to delete file and directory on hard disk

-- smem to delete file in RAM

-- sfill to delete "free space" on hard disk

-- sswap to delete all data from swap

ls -t1 | sed 1d | parallel -X rm
2010-01-28 12:28:18
Functions: ls sed
-1

xargs deals badly with special characters (such as space, ' and "). To see the problem try this:

touch important_file

touch 'not important_file'

ls not* | xargs rm

Parallel https://savannah.nongnu.org/projects/parallel/ does not have this problem.

ls -Q * | xargs -p rm
2010-01-27 02:46:49
User: temp_reg
Functions: ls xargs
1

ls -Q will show the filenames in quotes. xargs -p rm will print all the filenames piped from ls -Q and ask for confirmation before deleting the files.

without the -Q switch, if we have spaces in names, then the files won't be deleted.

find . -type d -empty -delete
for file in <directory A>/*; do rm <directory B>/`basename $file`; done
2009-05-04 12:44:50
User: jamiebullock
Functions: file rm
Tags: delete rm
10

This command is useful if you accidentally untar or unzip an archive in a directory and you want to automatically remove the files. Just untar the files again in a subdirectory and then run the above command e.g.

for file in ~/Desktop/temp/*; do rm ~/Desktop/`basename $file`; done
find ./ -mtime -5 | xargs rm -f
find /dir_name -mtime +5 -exec rm {} \
2009-03-08 12:03:44
User: eleffie
Functions: find rm
Tags: delete
5

This command will delete files i a given path (/dir_name) , which older than given time in days (-mtime +5 will delete files older than five days.