• 16 Examples of LS Command

    From Black Panther@CRBBSNET to All on Fri Aug 9 18:10:30 2019
    16 Practical Examples of Linux LS command for Beginners https://www.howtoforge.com/linux-ls-command/

    Whenever it's required to take a look at the contents of a directory on the command line in Linux, ls is the command that's used. It lists the contents sorted in alphabetical order.

    HTF@HowtoForge:~$ ls
    examples output.txt testfile4.log
    info file.txt test testfile5.tmp
    input testfile1.txt testfile6.dat
    new file.txt testfile2.txt
    new music.mp3 testfile3.txt

    While that's the tool's basic usage, it offers a plethora of features (in the form of command-line options) that help you in several circumstances. In this tutorial, we will discuss the usage of some of these command-line options through easy to understand examples.

    1. How to list hidden files/directories

    The ls command doesn't display hidden files/directories by default. However, you can force the tool to do that using the -a command-line option. So, to list all files/directories (including hidden ones - those with names beginning with a dot '.') in the current directory, run the following command:

    ls -a

    HTF@HowtoForge:~$ ls -a
    . input testfile2.txt
    .. new file.txt testfile3.txt
    examples new music.mp3 testfile4.log
    .hidden-dir output.txt testfile5.tmp
    .hidden-file test testfile6.dat
    info file.txt testfile1.txt

    2. How to skip current (.) and previous directory (..) entries in the output

    When using the -a command line options, the entries for the current and previous directory (. and .. respectively) also show up. If you want ls to not display these in output, use the -A command-line option instead.

    HTF@HowtoForge:~$ ls -A
    examples new file.txt testfile2.txt
    .hidden-dir new music.mp3 testfile3.txt
    .hidden-file output.txt testfile4.log
    info file.txt test testfile5.tmp
    input testfile1.txt testfile6.dat

    3. How to display files/directories in reverse order

    By default, the ls command displays the files alphabetically in the output. But if you want, you can also make the tool display files in reverse order. For this, you'll have to use the tool's -r command-line option.

    ls -r

    HTF@HowtoForge:~$ ls -r
    xargs Google Drive
    Videos gdrive
    Untitled 1.odt examples.desktop
    Templates echo.c
    Table screencasting tools.odt Downloads
    switches.odt Documents
    Public Desktop
    Pictures curl-7.50.3.tar.gz
    p7zip.odt curl-7.50.3
    ocamlfuse.odt commandline based questions.odt
    newfile.odt cli examples
    Music C
    linuxscreenrecorder.odt byzanz_window.py
    limits.c byzanz-gui
    grive.odt byzanz
    grive Augmented Reality.odt
    grep acd.byzanz
    HTP@HowtoForge:~$

    4. How to sort ls command output based on file extensions

    To sort the ls command output alphabetically based on file extensions, use the -X command-line option.

    ls -X

    Here's the above command in action:

    HTF@HowtoForge:~$ ls -X
    byzanz Music Augmented Reality.odt
    byzanz-gui Pictures commandline based questions.odt
    C Public grive.odt
    cli examples Templates linuxscreenrecorder.odt
    Desktop Videos ls command.odt
    dir xargs newfile.odt
    Documents curl-7.50.3 ocamlfuse.odt
    Downloads acd.byzanz p7zip.odt
    gdrive echo.c switches.odt
    Google Drive limits.c Table screencasting tools.odt
    grep examples.desktop Untitled 1.odt
    grive curl-7.50.3.tar.gz byzanz_window.py

    As you can see in the screenshot above, among the files with name containing an extension, 'echo.c' is listed first, and 'byzanz_window.py' is listed at the end.

    5. How to sort files based on modification time

    If you want to quickly know which files or directories were modified recently, then you can use the ls command's -t option.

    ls -t

    The -t command line option displays newest entries first.

    6. How to list subdirectories recursively

    If you want, you can make the ls command display contents of subdirectories as well. This can be done using the -R command line option.

    ls -R

    Following screenshot shows the above command in action:

    HTF@HowtoForge:~$ ls -R
    .:
    abc.txt dir1

    ./dir1:
    dir2 test.tmp

    ./dir1/dir2:
    dir3

    ./dir1/dir2/dir3:
    file.tmp testfile.txt
    HTF@HowtoForge:~$

    7. How to list filenames along with their inode numbers

    The ls command also lets you list file/directory names along with their respective inode numbers. The tool provides the -i command line option to access this feature.

    ls -i

    Here's the above command in action:

    HTF@HowtoForge:~$ ls -i
    162514 acd.byzanz 137472 grep
    135849 Augmented Reality.odt 416952 grive
    144496 byzanz 43 grive.odt
    162466 byzanz-gui 144738 limits.c
    417403 byzanz_window.py 130964 linuxscreenrecorder.odt
    144699 C 407183 Music
    144713 cli examples 144724 newfile.odt
    144721 commandline based questions.odt 131061 ocamlfuse.odt
    550188 curl-7.50.3 131055 p7zip.odt
    130921 curl-7.50.3.tar.gz 407184 Pictures
    407164 Desktop 407181 Public
    138481 dir 131057 switches.odt
    137470 Documents 144760 Table screencasting tools.odt 407169 Downloads 407179 Templates
    145110 echo.c 130924 Untitled 1.odt
    155107 examples.desktop 407185 Videos
    689 gdrive 144735 xargs
    451546 Google Drive

    As you can see in the screenshot above, the numbers on the left are inode numbers, while the names on the right are corresponding file/directory names.

    8. How to display detailed information about files and directories

    The ls command can also print detailed information about files and directories. To enable this output format (also known as long listing format), you need to use the -l command-line option.

    HTF@HowtoForge:~$ ls -l
    total 32
    drwxrwxr-x 3 himanshu himanshu 4096 Mar 24 14:16 examples
    -rw-rw-r-- 1 himanshu himanshu 0 Mar 24 14:30 info file.txt
    -rw-rw-r-- 1 himanshu himanshu 7 Mar 28 11:21 input
    -rw-rw-r-- 1 himanshu himanshu 0 Mar 23 15:53 new file.txt
    -rw-rw-r-- 1 himanshu himanshu 0 Mar 23 16:14 new music.mp3
    -rw-rw-r-- 1 himanshu himanshu 28 Mar 29 16:14 output.txt
    -rwxrwxr-x 1 himanshu himanshu 7359 Mar 28 14:47 test
    -rw-rw-r-- 1 himanshu himanshu 75 Mar 28 12:00 testfile1.txt
    -rw-rw-r-- 1 himanshu himanshu 7 Mar 29 16:04 testfile2.txt
    -rw-rw-r-- 1 himanshu himanshu 6 Mar 28 10:24 testfile3.txt
    -rw-rw-r-- 1 himanshu himanshu 0 Mar 20 21:04 testfile4.log
    -rw-rw-r-- 1 himanshu himanshu 0 Mar 20 21:04 testfile5.tmp
    -rw-rw-r-- 1 himanshu himanshu 0 Mar 20 21:04 testfile6.dat

    As you can see in the screenshot above, the output is divided into 8 columns. Here's what these columns convey:

    The first column shows file permissions
    The second column shows the number of hard links
    The third and the fourth ones are owner and group names
    fifth is the file size
    Sixth and seventh are date and time of last modification
    The last is the name of the file.

    9. How to display author information

    You can also ask the ls command to list each file's author when producing long format directory listings. For this, you have to combine --author option with -l.

    ls -l --author

    HTF@HowtoForge:~$ ls -l
    total 32
    drwxrwxr-x 3 himanshu himanshu himanshu 4096 Mar 24 14:16 examples
    -rw-rw-r-- 1 himanshu himanshu himanshu 0 Mar 24 14:30 info file.txt -rw-rw-r-- 1 himanshu himanshu himanshu 7 Mar 28 11:21 input
    -rw-rw-r-- 1 himanshu himanshu himanshu 0 Mar 23 15:53 new file.txt -rw-rw-r-- 1 himanshu himanshu himanshu 0 Mar 23 16:14 new music.mp3 -rw-rw-r-- 1 himanshu himanshu himanshu 28 Mar 29 16:14 output.txt
    -rwxrwxr-x 1 himanshu himanshu himanshu 7359 Mar 28 14:47 test
    -rw-rw-r-- 1 himanshu himanshu himanshu 75 Mar 28 12:00 testfile1.txt -rw-rw-r-- 1 himanshu himanshu himanshu 7 Mar 29 16:04 testfile2.txt -rw-rw-r-- 1 himanshu himanshu himanshu 6 Mar 28 10:24 testfile3.txt -rw-rw-r-- 1 himanshu himanshu himanshu 0 Mar 20 21:04 testfile4.log -rw-rw-r-- 1 himanshu himanshu himanshu 0 Mar 20 21:04 testfile5.tmp -rw-rw-r-- 1 himanshu himanshu himanshu 0 Mar 20 21:04 testfile6.dat

    As you can see in the screenshot above, the newly-added fifth column is for the author.

    Note: The official GNU website says, "In GNU/Hurd, file authors can differ from their owners, but in other operating systems the two are the same."

    10. How to print C-style escapes for non-graphic characters (like newlines and spaces)

    Suppose the current directory contains a file with a name having a new-line character. If you try listing the contents of that directory, that particular file name will be shown containing a question mark (?), which signifies a non- printable character.

    HTF@HowtoForge:~$ ls
    examples test
    foo?file testfile1.txt
    info file.txt testfile2.txt
    input testfile3.txt
    new file.txt testfile4.log
    new music.mp3 testfile5.tmp
    output.txt testfile6.dat

    However, the ls command provides an option that - when used - prints C-style escape characters for non-printable characters. The option in question is -b.

    HTF@HowtoForge:~$ ls -b
    examples test
    foo\nfile testfile1.txt
    info\ file.txt testfile2.txt
    input testfile3.txt
    new\ file.txt testfile4.log
    new\ music.mp3 testfile5.tmp
    output.txt testfile6.dat

    So you can see in the screenshot above, the -b option lead to ls printing '\n' for the newline character. Similarly, spaces get represented by '\[space]' in this mode.

    11. How to print file size in units other than bytes

    If you want to know the size of a file, you'll have to use the -l option (that enables long listing format) we discussed in point 3 above. The size gets displayed in bytes by default. However, there may be situations where-in you would want the size in kilobytes or megabytes.

    The ls command has an option that lets you specify the unit in which you want the file size to be displayed. The option in question is --block-size. For example, if you want the size to be displayed in kilobytes, you can run the following command:

    ls -l --block-size=k [file-name]

    HTF@HowtoForge:~$ ls -l --block-size=k 0-dblcmdr-first-launch*
    -rw-rw-r-- 1 himanshu himanshu 41K Jun 28 2016 0-dblcmdr-first-launch.jpg -rw-rw-r-- 1 himanshu himanshu 108K Jun 28 2016 0-dblcmdr-first-launch.png HTF@HowtoForge:~$

    Keep in mind that the size figure produced with -l command-line option is the actual file size, or the amount of data it contains. If, instead, you want to know the size of the file on the file system or allocated size (yes, it can be different), then you'll have to use the -s command.

    ls -s --block-size=k [file-name]

    HTF@HowtoForge:~$ ls -s --block-size=k 0-dblcmdr-first-launch*
    44K 0-dblcmdr-first-launch.jpg 108K 0-dblcmdr-first-launch.png HTF@HowtoForge:~$

    12. How to make ls display only filenames and file sizes in output

    If you want the ls command output to only contain file/directory names and their respective sizes, then you can do that using the -h option in combination with -l/-s command line option.

    For example:

    ls -s -h

    HTF@HowtoForge:~$ ls -s -h
    total 77M
    67M acd.byzanz 4.0K grep
    28M Augmented Reality.odt 4.0K grive
    4.0K byzanz 336K grive.odt
    4.0K byzanz-gui 4.0K limits.c
    4.0K byzanz_window.py 28K linuxscreenrecorder.odt
    4.0K C 108K ls command.odt
    4.0K cli examples 4.0K Music
    20K commandline based questions.odt 0 newfile.odt
    4.0K curl-7.50.3 204K ocamlfuse.odt
    8.5K curl-7.50.3.tar.gz 180K p7zip.odt
    12K Desktop 4.0K Pictures
    4.0K dir 4.0K Public
    4.0K Documents 216K switches.odt
    4.0K Downloads 16K Table screencasting tools.odt
    4.0K echo.c 4.0K Templates
    12K examples.desktop 300K Untitled 1.odt
    4.0K gdrive 4.0K Videos
    4.0K Google Drive 4.0K xargs

    13. How to make ls not list backup files in the output

    In Linux, backup files are represented with names ending with a tilde(~). The ls command provides an option (-B) using which you can ask the tool to not list backup files in output, in case that's what's needed.

    For example, the following screenshot shows two ls commands. The first one outputs the name of a backup file, but the second one (which uses the -B option) doesn't.

    HTF@HowtoForge:~$ ls | grep *~
    test_gprof.c~
    HTF@HowtoForge:~$ ls -B | grep *~
    HTF@HowtoForge:~$

    14. How to make ls specifically indicate the type of file in the output

    By default, the output that the ls command display is color-coded, where different colors represent different types of files. However, if you want, you can also have the tool append an indicator (for this purpose) to entries in output. This feature can be accessed using the -F option.

    Here's an example:

    HTF@HowtoForge:~$ ls -F
    examples/ new file.txt testfile1.txt testfile5.tmp
    foo?file new music.mp3 testfile2.txt testfile6.dat
    info file.txt output.txt testfile3.txt
    input test* testfile4.log
    HTF@HowtoForge:~$

    Observe the extra '/' with example directory, and '*' with test executable. Here's the complete list of indicators and along with information on what
    they indicate:

    @ indicates a symbolic link
    * indicates an executable
    = indicates a socket file
    | indicates a named pipe
    > indicates a door
    / indicates a directory

    15. How to change ls command output format

    If you want, you can also change the output format for the ls command. The tool's --format option lets you do this. This option requires you pass a value that indicates the type of output format you want. Available values are: 'verbose' or 'long', 'commas', 'horizontal' or 'across', 'vertical', and 'single-column'.

    For example, use --format=commas when you want a comma-separated output:

    HTF@HowtoForge:~$ ls --format=commas
    examples, foo?file, info file.txt, input, new file.txt,
    new music.mp3, output.txt, test, testfile1.txt, testfiles2.txt,
    testfile3.txt, testfile4.log, testfile5.tmp, testfile6.dat
    HTF@HowtoForge:~$

    Similarly, use --format=single-column when you want the output to be displayed in a single column:

    HTF@HowtoForge:~$ ls --format=single-column
    examples
    foo?file
    info file.txt
    input
    new file.txt
    new music.mp3
    output.txt
    test
    testfile1.txt
    testfiles2.txt
    testfile3.txt
    testfile4.log
    testfile5.tmp
    testfile6.dat
    HTF@HowtoForge:~$

    16. How to make ls hide a particular type of files

    If you want, you can also force the ls command to hide a particular type of files in output. The --hide option (which requires a shell pattern to be passed to it) lets you do this. For example, if you don't want the tool not to display .txt files in the output, here's the command you should run:

    ls --hide=*.txt

    The following screenshot shows the output both without and with the --hide option.

    HTF@HowtoForge:~$ ls
    examples new file.txt testfile1.txt testfile5.tmp
    foo?file new music.mp3 testfile2.txt testfile6.dat
    info file.txt output.txt testfile3.txt
    input test testfile4.log
    HTF@HowtoForge:~$ ls --hide=*.txt
    examples input test testfile5.tmp
    foo?file new music.mp3 testfile4.log testfile6.dat
    HTF@HowtoForge:~$

    Conclusion

    While ls is a very basic command, how effectively it can be used depends on the user's knowledge of the command line options the tool provides. Do try the options we've discussed here in tutorial, and in case of any doubt or query, head to its man page.


    ---

    |03B|09lack |03P|09anther|03(|09RCS|03)|07

    --- Mystic BBS v1.12 A43 2019/03/02 (Linux/64)
    * Origin: Castle Rock BBS - bbs.castlerockbbs.com - (77:1/102)
    Synchronet CRBBS QWK Test Site - bbs.castlerockbbs.com
  • From Netsurge@CRBBSNET to All on Wed Aug 14 04:09:08 2019
    On 2019-08-09 22:10:30 +0000, Black Panther said:

    While ls is a very basic command, how effectively it can be used depends on the user's knowledge of the command line options the tool provides. Do try
    the
    options we've discussed here in tutorial, and in case of any doubt or query, head to its man page.

    I tend to create aliases for the most used variations. For example, I
    have created an alias for "la" which doe ls -lah for me. The power of
    Linux is truly incredible.

    --
    frank // netsurge
    disksh0p!bbs % bbs.diskshop.ca % mystic goodness
    SciNet ftn hq % https://diskshop.ca/scinet

    .
    --- Unison/2.2
    * Origin: % SciNet ftn/nntp/email gateway % scinet-ftn.org % (77:77/5)
    Synchronet CRBBS QWK Test Site - bbs.castlerockbbs.com
  • From Nelgin@CRBBSNET to Netsurge on Wed Aug 14 21:22:23 2019
    Netsurge wrote:
    From: Netsurge <netsurge@scinet-ftn.org>

    On 2019-08-09 22:10:30 +0000, Black Panther said:

    While ls is a very basic command, how effectively it can be used depends on >> the user's knowledge of the command line options the tool provides. Do try
    the
    options we've discussed here in tutorial, and in case of any doubt or query, >> head to its man page.

    I tend to create aliases for the most used variations. For example, I
    have created an alias for "la" which doe ls -lah for me. The power of
    Linux is truly incredible.


    I always liked "ls -lsraS" = my fingers just sort of reel it off when I'm trying to look for the largest or smallest files.
    --- SBBSecho 3.08-Linux
    * Origin: End Of The Line BBS - endofthelinebbs.com (77:1/119)
    Synchronet CRBBS QWK Test Site - bbs.castlerockbbs.com