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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

Commands using ls from sorted by
Terminal - Commands using ls - 462 results
ls -ld *(/)
ls -l | grep ^d
2009-02-26 20:28:10
User: sysadmn
Functions: grep ls
1

Show only the subdirectories in the current directory. In the example above, /lib has 135 files and directories. With this command, the 9 dirs jump out.

ls -t1 | head -n1
for f in $(ls -d /base/*); do ln -s $f /target; done && ls -al /target
2009-02-26 04:48:19
Functions: ln ls
3

Symlinks all files in the base directory to the target directory then lists all of the created symlinks.

ls /sys/bus/scsi/devices
2009-02-25 18:49:27
Functions: ls
3

This will show all physically connected SATA (and SCSI) drives on your system. This is particularly useful when troubleshooting hard disks.... or when a mount point seems to be missing.

echo COMMAND | xargs -ixxx ps -C xxx -o pid= | xargs -ixxx ls -l /proc/xxx/cwd
find . -type f -mtime +7 -exec ls -l {} \;
2009-02-21 08:03:24
User: senses0
Functions: find ls
5

Find files that are older than x days in the working directory and list them. This will recurse all the sub-directories inside the working directory.

By changing the value for -mtime, you can adjust the time and by replacing the ls command with, say, rm, you can remove those files if you wish to.

N="filepath" ; P=/proc/$(lsof +L1 | grep "$N" | awk '{print $2}')/fd ; ls -l $P | sed -rn "/$N/s/.*([0-9]+) ->.*/\1/p" | xargs -I_ cat $P/_ > "$N"
2009-02-21 02:31:24
User: laburu
Functions: awk cat grep ls sed xargs
5

Note that the file at the given path will have the contents of the (still) deleted file, but it is a new file with a new node number; in other words, this restores the data, but it does not actually "undelete" the old file.

I posted a function declaration encapsulating this functionality to http://www.reddit.com/r/programming/comments/7yx6f/how_to_undelete_any_open_deleted_file_in_linux/c07sqwe (please excuse the crap formatting).

mplayer $(ls -t /tmp/Flash*|head -1)
2009-02-19 04:38:40
User: mulad
Functions: ls
1

Many sites with Flash video players will download video files to /tmp on Linux, with temporary filenames like "FlashbGTHm4". These will often play in mplayer, totem, or other movie playing software. You must first navigate to a video page, let it start loading, and then pause playback.

ls *(m-1)
ls **/fred*~*junk*/*
ls -ld **/*(/^F)
ls |while read line ; do mpg321 -w "$line.wav" "$line" ; done
ls -ad .*
2009-02-18 15:22:39
User: osantana
Functions: ls
-3

List only hidden files (or directories). You also can use:

ls -lad .*
ls -d */
2009-02-18 14:00:42
User: starchox
Functions: ls
8

This command would be useful when it is desirable to list only the directories.

Other options

Hidden directory

ls -d .*/

Other path

ls -d /path/to/top/directory/.*/

Long format:

ls -ld */
ls -l !* | /usr/bin/grep '^d'
ls -lrt
2009-02-17 19:51:08
User: guidouil
Functions: ls
2

Show you the list of files of current directory sorted by date youngest to oldest, remove the 'r' if you want it in the otherway.

find . -size +10240k -exec ls -l {} \; | awk '{ print $5,"",$9 }'|sort -rn > message.out
2009-02-17 19:39:56
User: rommelsharma
Functions: awk find ls
5

This command specifies the size in Kilobytes using 'k' in the -size +(N)k option. The plus sign says greater than. -exec [cmd] {} \; invokes ls -l command on each file and awk strips off the values of the 5th (size) and the 9th (filename) column from the ls -l output to display. Sort is done in reversed order (descending) numerically using sort -rn options.

A cron job could be run to execute a script like this and alert the users if a dir has files exceeding certain size, and provide file details as well.

ls -1 . dir2 dir3|sort|uniq -d
2009-02-17 16:29:03
User: tlknv
Functions: ls
1

cat file1 file2 file3|sort|uniq -d

finds the same lines in several files, especially in files with lists of files.

ls -1 /bin | xargs -l1 whatis 2>/dev/null | grep -v "nothing appropriate"
2009-02-17 14:46:01
User: stinger
Functions: grep ls whatis xargs
4

Get simple description on each file from /bin dir, in list form, usefull for newbies.

find / -type f -size +25M -exec ls -lh {} \; | awk '{ print $5 " " $6$7 ": " $9 }'
2009-02-16 12:27:48
User: darkon1365
Functions: awk find ls
1

Very useful for finding all files over a specified size, such as out of control log files chewing up all available disk space. Fedora Core x specific version.

ls -F | grep '\''\*'\'' | sed '\''s/\*$//'\
ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/'
2009-02-15 20:43:21
Functions: grep ls sed
55

Prints a graphical directory tree from your current directory

ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'
2009-02-13 19:20:54
User: iArno
Functions: awk ls
6

Do ls with permissions written in octal form.

ls -R | grep : | sed -e '\''s/:$//'\'' -e '\''s/[^-][^\/]*\//--/g'\'' -e '\''s/^/ /'\'' -e '\''s/-/|/'\''
2009-02-09 22:32:01
User: scw
Functions: grep ls sed
-3

Shows the current directory and those below it in a simple tree structure. Recommended use:

alias lt='$command_above'