Getting detailed information related to files on your storage is tricky if you do not know how to use the ls command. Here we'll discuss everything associated with the ls command on Linux, along with some various flags used with it. Show
The ls Command on LinuxThe ls command is used to list down all the files and folders present in your current working directory. You can also get a variety of information about the files using the same command. Since it is already included in the GNU core utilities package, you don't need to install any additional package on your system to use it. You can chain ls with other bash commands as well. For example, piping a grep statement with ls will allow you to search and filter the directory for specific files. How to Use the ls CommandThe basic syntax of the ls command is: One of the most simple use of the command is to list all the files and folders in your current working directory. If you execute the aforementioned statement in your system's root directory, you will see an output that looks something like this. Listing Files in a Specific DirectoryFor listing files that belong to a different folder (not the current working directory), you will have to pass the directory path along with the command name. To get the list of all the files present in the /boot directory: The output will now show the files and folders present in the directory name provided. Using the -F flag with the command will add a / character at the end of every directory. You can also pass multiple directories by separating the path names with a Space character. List Files in the Root DirectoryThe root directory contains all the other directories and files on your system. It is the top-most folder in your computer's directory-hierarchy. A root directory is generally denoted by the / character. It doesn't matter which directory you're in at the time of entering the command, the above-mentioned command will produce an output that lists all the sub-folders and files present inside the root directory. List Files in the Parent DirectoryA parent directory in Linux is a directory above the current directory. Let's take /usr/bin as an example. Here, /bin is your current working directory, and /usr is the parent directory. To get a list of all the files in a parent directory: Adding another .. will take you to the parent directory of the parent directory. For example, /var/log/old is your current working directory. ls .. will list the folders present in the /log directory whereas ls ../.. will provide you with a list of all the files and folders contained in the /var directory. List Files in the Home DirectoryThe home directory in Linux is denoted by the ~ character. Therefore, to list the content available in your home directory: List Only Directories (No Files)If for any reason you only want to list folders present in a directory, use the -d flag with the default ls command. List Files With Sub-DirectoriesUsing the * character with the ls command will provide you with a list of all the files and folders in the current working directory, along with the sub-directories as well. List Files RecursivelyUsing the -R flag with the default command will list down all the files and folders present inside a directory down to the last level. Note that you can also pass the directory path along with the recursive flag. This means that ls /usr/home -R is a valid command. List Files With Their SizeTo get the names of all the files along with their size, use the -s flag with the command. List Files With Detailed InformationThe -l flag allows you to get a list of a Linux directory's content with a detailed description of each entry. Following information are included in the output:
The first column is reserved for the file and folder permissions. The first character denotes the type of file and the next nine characters denote the permissions of the file. The various types of files that you'll often come across:
Talking about file permissions, the following characters are used in the output.
Let's take drw-r--r-- as an example. The first character tells that the entry is a directory. The following two characters denote that the current user has read and write permissions. The rest of the characters provide information on the file permissions for other users. List Files With Readable SizeThe -s command provides you with a numeric value associated with each entry. And as obvious, you wouldn't know what is the meaning of this value. Therefore, to list down files and their sizes in a readable manner, use the -lh flag along with the command. Size specifiers for bytes(B), megabytes(MB), gigabytes(GB), and terabytes(TB) are used in the output. List Hidden FilesThe default ls command doesn't include hidden files in the output. To list the content which is set as hidden by the user, pass the -a flag with the ls command. Piping ls With Grep CommandThe grep command is used to match patterns that follow a specific regular expression. You can chain this command with ls in order to search for files present in your system. In your root directory, type: This will list down all the files and folders that start with l character. You can also filter your files according to their extensions using grep. Sort Files by Time and DateTo list all the files and sort them according to the time and date of creation/modification, use the -t flag along with ls. Sort Files by SizeThe -S flag will allow you to sort the files and folders in accordance with their file size. By default, the files will be sorted in descending order (largest file first). However, you can easily reverse this behavior by adding r with the -S flag. List Files and Send Output to a FileUsing the > character, you can send the output of the ls command to any file. Later, you can read the content of the newly created file by typing cat ls-output.txt in your terminal. Displaying Contents of a Directory With ls CommandThe ls command is one of the most powerful commands provided to Linux users. To get the most out of your commands in terminal, you can try learning chaining commands together. You can even pipe the mv command for moving files with ls. The number one tip to get comfortable with Linux is to memorize some basic commands. This will surely help you in becoming much efficient and quick while using your system. |