Featured image of post  GNU Coreutils: ls, vdir, realpath

GNU Coreutils: ls, vdir, realpath

Let's understands the working of ls, vdir, and realpath, Coreutilites. GNU Coreutilties are the collection of numerous unix-based shell commands developed by GNU Project that provide essential file, text and shell based manipulation techniques and features.

It’s a part of multi blogs series, which will explain how to use GNU Core Utilities, useful for novice and professionals alike. GNU Coreutils are file, shell, and text manipulation commands/tools which are available on every GNU based Linux system.

In today’s blog, you will learn about Coreutils that are used in everyday file operations. Let’s begin:

πŸ“ Note

I will only explain those functions and commands which are in my opinion add value to everyday usage and management of the Linux Systems.

ls — list directory contents

To show the contents of a directory:

ls
  • This command will list only non-hidden directories and files.

List subdirectories and their content recursively:

ls -R
  • --recursive/-R Useful for investigation of unknown directories and their content

List entries by column (default):

ls -C

ls β€” Default column view

List entries by rows instead of default column view:

ls -x

ls β€” Row view

List one file per line:

ls -1

Show Hidden Entries

To list entries started with . (hidden):

ls -a
  • --all/-a lists hidden files and directories

The flag --all also shows . And .. Which represent current directory and parent directory respectively. To hide those:

ls -A

---almost-all/-A It doesn’t list implied . and ..

Show Long Listings

To show long listing format:

ls -l
  • -l Shows permissions, owner, group, size and timestamps, along with the entries

ls β€” Long listing format

To omit group information from long listing format, rest is the same as -l flag listing:

ls -o
  • -o/-lG/-l --no-group Exclude group information from long listing format

Owner can be omitted as:

ls -g

Print the author information of each file, when used with -l:

ls -l --author
  • It’s useful on multi-user systems to find out of who is the owner of the particular file lingering around.

Print C-Style backslash character (\n, \t, \r etc.) for non-printable and special characters.

ls -l -b
  • --escape/-b flag prints C-Style backslash characters

ls -l -b ls -l | Without Escape Characters

C-Style character map:

Escape Meaning
\n Newline
\t Tab
\r Carriage Return
\\ Literal Backslash
\0 Null character
\040 Space

Follow the symbolic link listed on the command line:

ls -l -H <SYMLINK>
  • The flags --dereference-command-line and -H serve the same purpose.
  • Only follow the symbolic links if file/DIR is given in CLI argument

-H flag require CLI argument

Follow the link and show information for the file itself not the symbolic link. This means ls will follow the link and display information about the target file or directory, not the link itself.

ls -l -L
  • The flags --dereference and -L are the same.

Original files info when given as CLI argument Target File Info | Remaining are DIRs

Print allocated size of each file in blocks:

ls -s
  • --size/-s shows the file size in bytes (blocks) which are 1024 bytes (1Β KiB) by default.

You can mention the specific unit yourself:

ls -s --block-size=M
  • K, M, G etc. are the powers of 1024(binary) or prefixes like KiB=K, MiB=M, GiB=G and so on.
  • KB, MB, GB etc. are the multiples of 1000 (decimal)

To show human-readable size (binary) with flags like -s and -l:

ls -sh
  • --human-readable/-h are the same

The flag --si prints the size information like -h but in decimals (powers of 1000):

ls --si -l
  • The --si flag should be used with either -s or -l flag

Sorting Order

To list directories before files:

ls --show-directories-first
  • It can be combined with -l -a flags

Reverse order while sorting:

ls -r
  • --reverse/-r Reverse the current order of listings

Sort by file size:

ls -S
  • --size/-S Sort the largest sized file first, doesn’t help with directories

To sort by time:

ls -t
  • -t Sort the newest modified file first
πŸ’‘ Tip

The sort by -t and -t with --time=mtime give the same results. The -t alone defaults to the last modification time.

Sort by time, based on birth time (creation time):

ls -t --time=birth
  • Combining with -l shows the creation/birth time
  • It will print newly created item first, flag -r can show the oldest birth first by reversing the order.

Showing oldest created entry first

Sorting based on when a file is last accessed(no modification or write):

ls -lt --time=atime
  • -u The shorter version of --time=atime

Last accessed entries with timestamp

🚨 INFO

On modern Linux systems, noatime mount option reduces the disk I/O operation by cutting down the access time updates. While relatime mount option only update access metadata under certain conditions.

Sort by last metadata change time:

ls -lt --time=ctime
  • The flag--time=ctime can be substituted to -c
πŸ’‘ Tip

It records changes to ownership and permissions along with creation and edit timestamps. While --time=mtime only records creation and edit timestamps.

Showing ownership change listed first

Timestamps Long Listing

πŸ“ Note

Timestamp information is only shown when using the long listing format with the flag -l, so all the below commands must be run with -l flag to actually see what’s happening with the timestamp information.

Timestamp Versions

You can see a different timestamp when listing with -l flag, which by-default shows the last modification times. You can achieve this without -t command which by-default sorts entries based on --time=WORD command.

Let’s list a last access time stamp which the files are last accessed by an editor, cat, or commands without modifying the entries order:

ls -l --time=atime
  • The short version is -u

Print the last metadata change timestamp:

ls -l --time=ctime
  • The flags --time=ctime and -c serve the same purpose

Timestamp Display Properties

By default, long listing shows the locale timestamp:

ls -l --time-style=locale

locale timestamps

To show full date and time with minutes precision:

ls -l --time-style=long-iso

long-iso timestamps

To list timestamps with nanosecond precision and time zone

ls -l --time-style=full-iso
  • The flags --time-style=full-iso and--full-time serve the same purpose

full-iso timestamps

IGNORE Entries via Pattern Matching

Basic ignore via -B flag:

ls -B
  • --ignore-backups/-B don’t list implied entries ending with ~
  • The ~ at the end of the file is used by different text editors and other programs to store backups of files.

Advance pattern matching:

ls --ignore=PATTERN
  • -I PATTERN/--ignore=PATTERN are the same

Basic Glob Patterns:

Pattern Matches
* Anything (any length)
? Any single character
[abc] One of these
*.txt Any .txt file

Hide all .text files:

ls -I *.txt

Hide all JSON backup files:

ls --ignore=*.json~

Hide all file1.txt, file2.txt and so on:

ls --ignore=file?.txt

Hide anything that has oct in their name anywhere:

ls --ignore=*oct*
  • It’s case-sensitive

ls — Exit Codes

Following are some useful exit codes:

0 OK

1 There is some minor problem, like subdirectory is not accessible etc.

2 Serious issue, i.e., cannot access CLI argument

πŸ“ Note

Modern Linux systems might just show the issue descriptively instead of an error code to simplify the troubleshooting process. To show an error code: ls [argument] ; echo $?

Useful Tips

Show colored output:

ls --color=auto
  • Shows different color for different entry types
  • Can be combined with other commands

Colored ls output

Show version information and exit:

ls --version

Distinguish between directories and files:

ls -F
  • Shows / with DIRs

Directories listing with /

Show help information:

ls --help

See manual pages:

man ls

Print index number of each file

ls -i
  • --inode/-i print the index number

vdir — List Directory Contents

The command vdir is equivalent to ls -l -b; that is, by default files are listed in the long format and special characters are represented by C-Style backslash escape sequences.

vdir

The advantage over ls -l -b is, vdir acts as a shorthand and chaining more flags becomes rather easy.

The size information (in powers of 1000):

vdir --si

Instead of writing:

ls -l -b --si
πŸ’‘ Tip

You can chain other ls flags with vdir, i.e., --time, --block-size, --full-time, --reverse, --ignore=PATTERN and so on. The vdir command has basically all the same flags and options as ls.

The command realpath prints the resolved symbolic link paths.

Resolve symlinks as encountered (default):

realpath /path/to/file
  • The flag -P/--physical prints the same information as realpath alone. It defaults to this, so we don’t need to write the whole flag.

The flag -E, all but the last component must exist (default):

realpath -E /path/to/file
  • --canonicalize/-E
  • If parent directory exists, it will show the path as is, doesn’t matter if symlink file exists or not
  • That’s the default flag for realpath

ghost.txt doesn’t exists but still prints the path Default behavior is same as flag -E

It will only throw error if parent directory for the given file doesn’t exist

  • In our examples’ ghost.txt doesn’t exist but the parent directory /tmpexist, so no error
  • In the following picture, parent directory ../ghost/ doesn’t exist, so -E flag throws an error.

Shows an error when the file/link doesn’t exist:

realpath -e /path/to/file
  • Only prints the path, if symbolic link exists for the given path.
  • The flags -e/--canonicalize-existing are the same.

All components of the path must exist The whole path is valid

The -m/--canonicalize-missing flag doesn’t resolve anything, so no path or file existence is needed.

realpath -m /path/to/file

The whole path doesn’t exist

πŸ“ Note

There is a readlink command, which has almost similar functionality. It might be useful in shell scripts. Check out the docs.

References

Licensed under CC BY-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy