Commands using ls (517)

  • Returns a list, with attributes (think `ls -l`), in reverse chronological order. N is a single numeric parameter. Robust against unfriendly filenames and directory structures. Show Sample Output


    0
    nmf() { find . -type f -printf '%T@ ' -print0 -printf '\n' | sort -rn | head -"$1" | cut -f2- -d" " | tr -d "\0" | tr "\n" "\0" | xargs -0 ls -Ulh; }
    incidentnormal · 2016-03-04 14:53:14 12
  • I've shortened it to: lsc PATH | l ... by adding ... alias lsc="ls --color" ... and ... alias l="less -R" ... to my ~/.bashrc file


    0
    ls --color PATH | less -R
    kevjonesin · 2016-03-07 13:46:02 13
  • Grabs the first JSON file in the directory, reads its keys, prints TSV, then prints all the json files' values as TSV. Nested objects appear as json. Unhappy times if your json has literal tabs in it. Show Sample Output


    0
    jq -r 'keys | join("\t")' $(ls -f *.json | head -1) && jq -Sr 'to_entries | [ .[] | .value | tostring ] | join("\t")' *.json
    drjeats · 2016-04-08 23:30:30 12
  • It will print a compact ls -la list with the directories at the beginning. --almost-all - do not list implied . and .. --group-directories-first - group directories before files --color - colorize the output --no-group - in a long listing, don't print group names --human-readable - print human readable sizes (e.g., 1K 234M 2G) --classify - append indicator (one of */=>@|) to entries If you want to see the owner: ls -l --almost-all --group-directories-first --color --no-group --human-readable --classify Show Sample Output


    0
    ls -g --almost-all --group-directories-first --color --no-group --human-readable --classify
    icatalina · 2016-04-15 17:12:35 13
  • This command will find any named file types in / between two dates then will list all the metadata of those files in long format and human readable form. Adding a 't' flag to the ls command sorts the files by modified time. After all that the head -5 lists the first 5 which can be changed.


    0
    ls -laht `find / -name "*.*" -type f -newermt "2016-04-05" ! -newermt "2016-04-10"`|head -5
    ubercoo · 2016-04-19 14:26:23 10

  • 0
    ls -l --color | less -R
    Sparkette · 2016-05-07 17:19:56 11

  • 0
    man $(ls /bin | shuf -n1)
    jubnzv · 2016-06-28 18:34:46 12
  • Pipes the output of ls to espeak Also works nice with fortune fortune | espeak


    0
    ls | espeak
    BigZ · 2016-08-02 17:54:39 13
  • I seem to do this compulsively every time I change directories, sometimes even when I don't, even if I know exactly what I need to do. (Don't worry, the sample output is just an exaggeration. :) Show Sample Output


    0
    grep -cx ls ~/.bash_history
    Sparkette · 2016-09-14 17:06:59 17
  • ls -l outputs long listing of files to awk, which sorts the output to include all lines that have the executable bit set (-x.), but excludes (!) the lines that have the directory bit set (drw), then prints the results to the screen. Show Sample Output


    0
    ls -l|awk ''/-x./' && !'/drw/' {print}'
    PCnetMD · 2016-09-21 14:42:10 15

  • 0
    find . -mtime +30 -exec ls -all "{}" \; | awk '{COUNTER+=$5} END {SIZE=COUNTER/1024/1024; print "size sum of found files is: " SIZE "MB"}'
    breign · 2016-10-28 08:05:57 16
  • Resume incomplete youtube-dl video files. Assuming mp4 format here.


    0
    ls *.part | sed 's/^.*-\(.\{11,11\}\)\.mp4\.part$/\1/g' - | youtube-dl -i -f mp4 -a -
    agp · 2017-02-28 23:31:55 21
  • All the other example fail when running in a folder containing too many files due to * being saturated. This command does not use *, allowing me to run it in one folder containing over 300000 audio files. As to running on so many files, I used GNU parallel in order to spawn as many processes as cores, tremendously fasting up the process. Show Sample Output


    0
    ls|grep ".wav"|parallel -j$(nproc) soxi -D {}|awk '{SUM += $1} END { printf "%d:%d:%d\n",SUM/3600,SUM%3600/60,SUM%60}'
    jupiter126 · 2017-05-02 21:37:24 20

  • 0
    ls -t /mcdata/archive/learn/backup-moodle2-course-* | tail -n +11 | xargs -I {} rm {}
    tlezotte · 2017-05-04 13:50:02 17
  • Show file count into directories. Usefull when you try to find hugh directories that elevate system CPU (vmstat -> sy) Show Sample Output


    0
    find / -type d | while read i; do ls $i | wc -l | tr -d \\n; echo " -> $i"; done | sort -n
    Zort · 2017-05-12 00:02:43 19
  • It works extremely fast, because it calculates md5sum only on the files that have the same size and name. But there is nothing for free - it won't find duplicates with the different names. Show Sample Output


    0
    find -type f -printf '%20s\t%100f\t%p\n' | sort -n | uniq -Dw121 | awk -F'\t' '{print $3}' | xargs -d '\n' md5sum | uniq -Dw32 | cut -b 35- | xargs -d '\n' ls -lU
    ant7 · 2017-05-21 02:26:16 16

  • 0
    find . -name '*.log' | xargs ls -hlt > /tmp/logs.txt && vi /tmp/logs.txt
    zluyuer · 2017-07-07 05:13:51 20

  • 0
    AWS_DEFAULT_REGION="sa-east-1" jungle ec2 ls | grep midas | sort | cut -f4 | xargs -I {} ssh ubuntu@{} sudo apt-get install ntp -y
    xymor · 2017-11-22 19:20:08 19
  • To HUNT for all the important stuffs. TRUST EL TRAPPER Works every time!


    0
    ls -ltrapR
    K33st · 2018-03-19 18:15:51 25

  • 0
    ls -tr ~/Downloads/*.pdf|tail -1
    masroor · 2018-05-14 14:01:55 136
  • I couldn't find movie library on any of the SQLlite Stremio databases, but on ~/.config/stremio/backgrounds2 the background image filenames corresponds to IMDB URL. So I foreach files and wget HTML title of each movie and save it to a file. This will retrieve all movie names, not just the Library.


    0
    time for movie in $(ls -1 $HOME/.config/stremio/backgrounds2 | sort -u);do echo "https://www.imdb.com/title/$movie/" | wget -qO- -O- -i- --header="Accept-Language: en" | hxclean | hxselect -s '\n' -c 'title' 2>/dev/null | tee -a ~/movie-list.txt ; done
    pabloab · 2018-08-16 06:11:41 286
  • On Linux, use watch -n 1 ls path/to/dir H/t: https://stackoverflow.com/a/9574123/805405 Show Sample Output


    0
    while :; do clear; ls path/to/dir | wc -l; sleep 1; done
    minademian · 2018-12-13 17:48:24 229
  • find all files that have 20 or more MB on every filesystem, change the size and filesystem to your liking


    0
    find / -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -nrk 2,2
    Marius · 2019-07-08 21:04:09 37

  • -1
    cleartool co -nc `cleartool ls -recurse | grep "hijacked" | sed s/\@\@.*// | xargs`
    ultrahax · 2009-02-06 00:03:51 51
  • On my music directory, I create variable that contains all mp3s files, then I play them with mpg123. -C options enable terminal control key, s for stop, p for pause, f for forward to next song.


    -1
    PLAYLIST=$(ls -1) ; mpg123 -C $PLAYLIST
    servermanaged · 2009-03-19 17:20:28 12
  • ‹ First  < 13 14 15 16 17 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands


Check These Out

worse alternative to
worse alternative to ctrl+r: grep the history removing duplicates without sorting (case insensitive search).

Convert seconds to [DD:][HH:]MM:SS
Converts any number of seconds into days, hours, minutes and seconds. sec2dhms() { declare -i SS="$1" D=$(( SS / 86400 )) H=$(( SS % 86400 / 3600 )) M=$(( SS % 3600 / 60 )) S=$(( SS % 60 )) [ "$D" -gt 0 ] && echo -n "${D}:" [ "$H" -gt 0 ] && printf "%02g:" "$H" printf "%02g:%02g\n" "$M" "$S" }

Download all images from a 4chan thread
Useful for ripping wallpaper from 4chan.org/wg

resume other user's screen session via su, without pty error
Normally, if you su to another user from root and try to resume that other user's screen session, you will get an error like "Cannot open your terminal '/dev/pts/0' - please check." This is because the other user doesn't have permission for root's pty. You can get around this by running a "script" session as the new user, before trying to resume the screen session. Note you will have to execute each of the three commands separately, not all on the same line as shown here. Credit: I found this at http://www.hjackson.org/blog/archives/2008/11/29/cannot-open-your-terminal-dev-pts-please-check.

Download SSL server certificate with opsnessl
Useful when we need to create new certificate for site when current one is near expiry. This downloaded cert can be used to provide organisational data directly to new cert. using below command. e.g. openssl x509 -x509toreq -in /tmp/example.com.cert -out example.com.csr -signkey example.com-key.pem

ssh to machine behind shared NAT
Useful to get network access to a machine behind shared IP NAT. Assumes you have an accessible jump host and physical console or drac/ilo/lom etc access to run the command. Run the command on the host behind NAT then ssh connect to your jump host on port 2222. That connection to the jump host will be forwarded to the hidden machine. Note: Some older versions of ssh do not acknowledge the bind address (0.0.0.0 in the example) and will only listen on the loopback address.

Get your external IP address without curl
Curl is not installed by default on many common distros anymore. wget always is :) $ wget -qO- ifconfig.me/ip

processes per user counter
No need for sort

Copy one file to multiple files
Copies file.org to file.copy1 ... file.copyn

power off system in X hours form the current time, here X=2


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: