Some Noticeable Information
The ls command is a powerful tool that can be used to list files and directories.
There are numerous practical ls options and parameters that can enhance its functionality and improve its usability. One effective command to view all contents within a folder is ls -la.
ls Lists Files and Directories
The Linux ls command is often overlooked and used routinely without much consideration. However, it is worth giving it some attention as it offers a variety of helpful options that can greatly enhance your command-line skills.
The ls command, known to be the initial encounter for most Linux users, is frequently utilized by those who navigate the command line on a daily basis, often without much thought. This explains the lack of awareness among users regarding the extent of its capabilities. The command allows us to list the contents of a directory, while also providing the ability to view file permissions through the long format. However, it is often overlooked that the ls command provides numerous options, which can make it challenging to identify the most useful ones. Furthermore, once these options are discovered, remembering them becomes an additional obstacle.
Useful combinations of options and parameters for the ls command are ideal for creating aliases. In fact, in most distributions, the "naked" ls command that you are familiar with is actually an alias. The type command is useful for displaying the original definition of aliases. Let's examine the definition of ls:type ls
The ls command automatically includes the --color=auto parameters, which are responsible for the varying colors of different file types in the listings.
Simple ls Listings
Everyone who's spent some time using the Linux terminal knows that, by default, ls lists the files and directories in the current directory.
ls
If you want to have your listing produced ina single column, use the -1 (one file per line) option:
ls -1
We'll discuss that weird-looking filename at the top of the listing in a minute.
Using ls on Different Directories
To list the files in a directory other than the current directory using ls, simply provide the path to the desired directory on the command line. Additionally, you can specify multiple directories by separating them with a space. For instance, in the given example, we are instructing ls to list the files in two directories named "Help" and "gc_help."
New
To list files in directories other than the current directory, use the ls command followed by the desired directory path. You can also include multiple directories by separating them with a space. In this case, we are using ls to display the files in two directories: "Help" and "gc_help."
When ls has listed the contents of the first directory it lists the contents of the second. It prints the name of each directory as it processes them:
Using File Patterns
For selectively listing a set of files, utilize pattern matching. By employing the question mark "?" you can represent any single character, whereas the asterisk "*" can indicate any string of characters. To obtain a list of files or directories with names starting with "ip_", follow this format:
Use "ls ip_*"
To list files that have ".c" extensions, use this format:
ls *.c
You can also use ls with grep , and use grep's pattern matching capabilities. Let's look for any files that have the string "_pin_" in their name:
ls | grep _pin_
This is almost the same as using ls on its own, with two wildcards:
ls | grep _pin_
ls *_pin_*
Why almost the same? Note the different layouts. grep forces the output to a single filename per line format.
Non-Printing Characters
You may come across a situation where a file contains non-printing or control characters in its filename. This commonly occurs when extracting an archive downloaded from the web or fetching a git repository, where the original creator unintentionally made an error while naming the file.
Our weird file is one of these:
If we look at it in the file browser and press "F2" to rename it, the non-printing characters are represented by a strange symbol.
To view the actual file names, you can utilize the -b (escape) option. This option will instruct ls to display the control characters using the escape sequences of the C programming language.
For example, to display all files starting with 'a', you can use the command ls -b a*.
The mysterious character is revealed to be a newline character, represented in C as "\n."
Ignoring Files
To have certain files omitted from a listing, use the --hide option. Suppose you don't want to see the backup ".bak" files in the listing. You could use this command:
ls
ls --hide=*.bak
The ".bak" files are not included in the second listing.
The Long Format Listing
The -l (long listing) option causes ls to provide detailed information about each file.
ls -l
Let's go through the information provided here, as there is quite a lot.
The initial display from the "ls" command shows the cumulative size of all files in the listing. Subsequently, each file or directory is presented on a separate line.
The first set of ten letters and dashes are the file type and the owner, group and other file permissions.
The very first character represents the file type. It will be one of:
-: A regular file.
b: A block special file.
c: A character special file.
d: A directory.
l: A symbolic link.
n: A network file.
p: A named pipe.
s: A socket.
The following nine characters consist of three consecutive groups of three characters. Each group signifies the read, write, and execute permissions respectively. An "r," "w," or "x" will be present if the permission is granted, while a hyphen (-) will indicate that the permission is not granted.
The initial set of three characters represents the permissions for the file owner. The second set of three characters pertains to group members, and the final set of three characters corresponds to others.
Sometimes, the execution permission for the owner is denoted by an "s," which signifies the setuid bit. Its presence implies that the file executes with the privileges of the file owner, rather than the user who executes the file.
Similarly, the execution permission for the group can be denoted by an "s" as well, known as the setgid bit. When applied to a file, it indicates that the file will execute with the privileges of the owner's group. In the case of a directory, any files created within it will inherit their group permissions from the directory itself, rather than from the user who creates the file.
The t in the permission represents the execution permission for others, known as the sticky bit. It is commonly applied to directories. When set, only the file owner, directory owner, or root user have the ability to rename or delete files in the directory, regardless of the write and executable privileges set for the files. A typical use of the sticky bit is found in folders like "/tmp" that are writable by all users. Its presence guarantees that users and any processes launched by them can only rename or delete their own temporary files.
The sticky bit can be observed on the "/tmp" directory. The use of the -d (directory) option is noted, which prompts ls to provide details about the directory instead of just reporting on the files within it.
ls -l -d /tmp
The number indicated after the permissions represents the count of hard links pointing to the file or directory. Normally, this number is one for a file, but it can increase if additional hard links are established. A directory typically has a minimum of two hard links. One of these links refers to the directory itself, while the other is its reference in the parent directory.
The name of the owner and group are displayed next. They are followed by the file size and the date of the last modification of the file. Finally, the filename is given.
Human Readable File Sizes
Having the file sizes in bytes is not always convenient. To see the file sizes in the most appropriate units (Kilobytes, Megabytes, etc.) use the -h (human-readable) option:
ls -l -h
Showing Hidden Files
To see hidden files, use the -a (all) option:
ls -l -a
The two entries "." and ".." represent the current directory and the parent directory, respectively. A file called ".base_settings" is now visible for the first time.
Omitting . and .. from Listings
If you don't want your listing cluttered up with the "." and ".." entries, but you do want to see hidden files, use the -A (almost all) option:
ls -l -A
The hidden file is still listed, but the "." and ".." entries are suppressed.
Listing Directories Recursively
To have ls list the files in all subdirectories use the -R (recursive) option
ls -l -R
ls works its way through the entire directory tree below the starting directory, and lists the files in each subdirectory.
Displaying the UID and GID
To have the user ID and group ID displayed instead of the user name and group name, use the -n (numeric uid and gid) option.
ls -n
Sorting The Listings
Sort the listing by extension, file size, or modification time. While these options are not necessary when using the long listing format, it is typically more logical to do so. If you choose to sort by file size, it is advisable to display the file sizes in the listing. However, when sorting by extension type, the long listing format becomes less crucial. To sort by extension, simply utilize the -X option.
ls -X -1
The directories are listed first (no extensions at all) then the rest follow in alphabetical order, according to the extensions.
To sort by file size, use the -S (sort by file size) option.
ls -l -h -S
The sort order is largest to smallest.
To sort the listing by modification time, use the -t (sort by modification time) option.
ls -l -t
The listing is organized based on the time of modification.
If the file's modification time falls within the current year, the displayed information includes the month, day, and time. If the modification date is from a previous year, the displayed information includes the month, day, and year.
A quick way to get the newest and oldest files in a directory is to use ls with the head and tail commands.
To get the newest file or directory, use this command:
ls -t | head -1
To get the oldest file or directory, use this command:
ls -t | tail -1
To Reverse the Sort Order
To reverse any of the sort orders, use the -r (reverse) option.
ls -l -h -S -r
The listing is now ordered from the smallest file to the largest file.
And there's more
For a better understanding of the available options, refer to the ls man page. It offers a wide range of choices, including some that cater to less common scenarios. Although you may not frequently encounter these options, having knowledge of them can prove beneficial on certain occasions.
To obtain file timestamps with the utmost precision offered by Linux, utilize the full-time option.
ls --full-time
Perhaps you want to see the inode number of the files? Use the inode option:
ls -i --classify
/: A directory.
@: A symlink.
|: A named pipe.
=: A socket.
*: An executable files
ls -F
Do some digging. You'll find that ls is a rich vein, and you'll keep turning up gems.
Editor's P/S
As a Gen Z netizen, I find the ls command to be an essential tool for navigating the Linux terminal. Its versatility and simplicity make it a powerful tool for quickly and easily listing files and directories. I appreciate the ability to use the ls command to view file permissions, sort listings, and ignore certain files.
Additionally, the ability to use the ls command in combination with other commands, such as grep, makes it even more powerful. Overall, I find the ls command to be an invaluable tool for managing files and directories on the Linux terminal.