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 using ls from sorted by
Terminal - Commands using ls - 453 results
ls -la | grep $(date +%Y-%m-%d) | egrep -v -e '\.{1,2}' | sed "s/.*\:[0-9]\{2\} \(.\+\)$/\\1/g"
today=`date +%d`; ls -ltr | rm -f `nawk -v _today=$today '{ if($5 != 0 && $7 < _today) { print $9 } }'`
2010-07-29 13:47:19
User: alex__
Functions: ls rm
0

Delete all files that its size it's different than 0 and older than actuall day.

ls -l --time-style=+%Y-%m-%d | awk "/$(date +'%Y-%m-%d')/ {print \$7}"
2010-07-29 05:30:29
Functions: awk ls
1

This version eliminates the grep before the awk, which is always good. It works for GNU core utils and ensures that the date output of ls matches the format in the pattern match, regardless of locale, etc.

On BSD-based systems, you can easily eliminate both the grep and the awk:

find . -maxdepth 1 -Btime -$(date +%kh%lm) -type f

for file in $(ls /usr/bin ) ; do man -w $file 2>> nomanlist.txt >/dev/null ; done
2010-07-26 19:39:53
User: camocrazed
Functions: file ls man
Tags: man
-2

This takes quite a while on my system. You may want to test it out with /bin first, or background it and keep working.

If you want to get rid of the "No manual entry for [whatever]" and just have the [whatever], use the following sed command after this one finishes.

sed -n 's/^No manual entry for \(.*\)/\1/p' nomanlist.txt
rm $( ls | egrep -v 'abc|\s' )
2010-07-18 10:59:15
User: dbbolton
Functions: egrep ls rm
Tags: grep rm
-1

Really, you deserve whatever happens if you have a whitespace character in a file name, but this has a small safety net. The truly paranoid will use '-i'.

url=http://www.youtube.com/watch?v=V5bYDhZBFLA; youtube-dl -b $url; mplayer $(ls ${url##*=}*| tail -n1) -ss 00:57 -endpos 10 -vo gif89a:fps=5:output=output.gif -vf scale=400:300 -nosound
2010-07-18 02:11:39
User: zed
Functions: ls tail
12

requires "youtube-dl" -- sure you can do this with wget and some more obscurity but why waste your time when this great tool is available?

the guts consist of mplayer converting a video to a gif -- study this command and read the man page for more information

mplayer video.flv -ss 00:23 -endpos 6 -vo gif89a:fps=5:output=output.gif -vf scale=400:300 -nosound

generates a 6 second gif starting at 23 seconds of play time at 5 fps and a scale of 400x300

start time (-ss)/end time (-endpos) formats: 00:00:00.000

end time should be relative to start time, not absolute. i.e. -endpos 5 == seconds after 0:42 = 0:47 end point

play with fps and scale for lower gif sizes

the subshell is a solution for the -b flag on youtube-dl which downloads the best quality video, sometimes, which can be various video formats $(ls ${url##*=}*| tail -n1)

ls -l $(find ./ -type l | perl -ne 'chomp; if (-d) { print "$_\n" }')
2010-07-16 19:31:28
User: rwadkins
Functions: find ls perl
-1

This will list all symlinks that are directories under the current directory. This will help you distinguish them from regular files.

ls -l `which foo`
2010-07-09 01:34:02
User: adeverteuil
Functions: ls
1

You may also use the $(which foo) variant instead of backticks. I personnaly have an alias ll='ls -l'.

fortune | cowsay -f $(ls /usr/share/cowsay/cows/ | shuf -n1)
2010-07-08 02:57:52
User: zed
Functions: ls
8

You need to have fortune and cowsay installed. It uses a subshell to list cow files in you cow directory (this folder is default for debian based systems, others might use another folder).

you can add it to your .bashrc file to have it great you with something interesting every time you start a new session.

bsro3 () { P=`pwd`; S=$1; R=$2; ls *.odt > /dev/null 2>&1; if [[ $? -ne 0 ]]; then exit 1; fi; for i in *.odt; do mkdir ${P}/T; cd ${P}/T; unzip -qq "$P"/"$i"; sed -i "s/$S/$R/" ${P}/T/content.xml; zip -qq -r "$P"/"$i" *; cd ${P}; rm -rf ${P}/T; done; }
2010-06-30 04:43:54
User: danpos
Functions: cd exit ls mkdir rm sed
2

This function does a batch edition of all OOO3 Writer files in current directory. It uses sed to search a FOO pattern into body text of each file, then replace it to foo pattern (only the first match) . I did it because I've some hundreds of OOO3 Writer files where I did need to edit one word in each ones and open up each file in OOO3 gui wasn't an option. Usage: bsro3 FOO foo

find / -type f -name *.tar.gz -size +10M -exec ls -l {} \;
2010-06-29 12:39:02
User: 0disse0
Functions: find ls
Tags: find ls exec rm type
0

Please be careful while executing the following command as you don?t want

to delete the files by mistake. The best practice is to execute the same

command with ls ?l to make sure you know which files will get deleted when

you execute the command with rm.

while :;do if [ ! $(ls -l commander |cut -d ' ' -f5) -eq 0 ]; then echo "Ran command: $(less commander) @ $(date +%D) $(date +%r)" >> comm_log;"$(less commander)";> commander;fi;done
2010-06-15 01:20:27
User: evil
Functions: cut echo ls
0

This is a simple solution to running a remote program on a remote computer on the remote display through ssh.

1. Create an empty 'commander' file in the directory where you intend on running these commands.

2. Run the command

3. Hop on another computer and ssh in to the PC where you ran the command

4. cd to the directory where the 'commander' file is.

5. Test it by doing the following: echo "xeyes" > commander

6. If it worked properly, then xeyes will popup on the remote computer.

Combined with my other one liner, you can place those in some start-up scripts and be able to screw with your wife/daughter/siblings, w/e by either launching programs or sending notifications(my other one liner).

Also, creates a log file named comm_log in working directory that logs all commands ran.

while : ; do if [ ! $(ls -l commander | cut -d ' ' -f5) -eq 0 ]; then notify-send "$(less commander)"; > commander; fi; done
2010-06-13 18:45:02
User: evil
Functions: cut ls
2

Run this command when you are physically at the computer you wish to send pop-up messages to. Then when you ssh in to it, you can do this: echo "guess who?" > commander

guess who? will then pop up on the screen for a few moments, then disappear. You will need to create the commander file first. I mess with my wife all the time with this. i.e. echo "You have given the computer a virus. Computer will be rendered useless in 10 seconds." > commander

lol

find directory -maxdepth 1 -type f | xargs ls -l | awk 'BEGIN { SUM=0} { SUM+=$5 } END { print SUM/2^20 }'
ls -l directory | awk 'BEGIN { SUM=0 } { SUM+=$5 } END { print SUM/1024/1024"M" }'
find / -type f -size +500000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
find . -type f -exec ls -s \{\} \; | sort -n
2010-06-02 11:03:31
User: Insti
Functions: find ls sort
-2

Finds all files below the current directory.

Orders the result from smallest to largest.

Good for finding the largest files in the tree.

find . -type f | xargs ls -ltrhg
2010-05-28 01:23:53
User: emacs
Functions: find ls xargs
-1

find and normal files and list them sorting with modification time without group

l: with detailed information

t: sort with modification time

r: reverse order

h: show file's size in human-readable format, such as K(kilobytes), M(megabyes) etc.

g: do not show group

ls -rl --time-style=+%s * | sed '/^$/,/^total [0-9]*$/d' | sort -nk6
find . -type f -mtime -14 -exec ls -ltd \{\} \; | less
find . -type f -exec ls -tr {} +
2010-05-27 14:52:28
Functions: find ls
-2

List all files from the current directory and subdirectories, sorted by modification time, oldest first.

ls -lt | more
2010-05-27 12:44:39
User: eludom
Functions: ls
3

Simple but useful; list files in the current directory in mtime order. Useful if you've been working on something and then take a day or two off.

rm-but() { ls -Q | grep -v "$1" | xargs rm -r ; }
2010-05-13 09:28:56
User: sata
Functions: grep ls rm xargs
0
rm-but() { ls -Q | grep -v "$1" | xargs rm -r ; }

Add this to your .bashrc file.

Then whenever you need to remove all files/directories but one from present working directory. Run:

rm-but <important-file-or-directory>

Notes:

1. This doesn't affect the hidden files.

2. Argument is actually as string. And all files/directories having this string in there name are left untouched.

find / -type f -size +512000 | xargs ls -lh | awk '{ print $5 " " $6$7 ": " $9 }'
2010-05-12 17:21:12
User: johnss
Functions: awk find ls xargs
-1

This is an updated version that some one provided me via another "find" command to find files over a certain size. Keep in mind you may have to mess around with the print values depending on your system to get the correct output you want. This was tested on FC and Cent based servers. (thanks to berta for the update)

s3cmd ls s3://bucket.example.com | s3cmd del `awk '{print $4}'`