LFCS — Linux Foundation Certified Systems Administration Preparatory Course by Emmanuel Odenyire
Lesson 3 — Using Essential File Management Tools
Topic: Listing Files with ls
Introduction
The “ls” command is one of the most basic commands in Ubuntu and Linux in general. It is used to list the files and directories in the current working directory. In this tutorial, we will go over the various options and arguments that can be used with the “ls” command.
Basic usage: To list the files and directories in the current working directory, simply type “ls” in the terminal and press enter:
ls
This will give you a list of all the files and directories in the current directory.
Options: The “ls” command has several options that can be used to modify its behavior. Here are some of the most commonly used options:
“-l” (long format): This option lists the files and directories in a long format, which includes information such as file permissions, owner, group, size, and modification time. To use this option, simply type:
ls -l
“-a” (all files): This option lists all files and directories, including hidden files (files that start with a dot “.”). To use this option, simply type:
ls -a
“-h” (human-readable sizes): This option displays file sizes in a human-readable format, such as “1K” for 1 kilobyte. To use this option, simply type:
ls -h
“-S” (sort by file size): This option sorts the files and directories by file size, with the largest files first. To use this option, simply type:
ls -S
“-t” (sort by modification time): This option sorts the files and directories by modification time, with the most recently modified files first. To use this option, simply type:
ls -t
Arguments: In addition to options, the “ls” command can also take arguments, which specify the files or directories to list. Here are some examples:
List a specific directory:
ls /path/to/directory
List multiple directories:
ls /path/to/directory1 /path/to/directory2
List files with a specific extension:
ls *.txt
List files with a specific prefix:
ls file*
Here’s some additional and useful information about using the “ls” command in Ubuntu:
- Displaying permissions and ownership with “ls -l”: When using the “ls -l” command, you’ll see a string of characters on the left side of each file and directory name. This string represents the permissions for the file or directory, and is made up of 10 characters:
The first character indicates the file type (e.g., “-” for a regular file, “d” for a directory).
The next three characters indicate the owner’s permissions (e.g., “rwx” for read, write, and execute permissions).
The next three characters indicate the group’s permissions.
The final three characters indicate the permissions for all other users.
You’ll also see the owner and group names listed next to the permissions string.
2. Using “ls” with wildcards: As mentioned in the previous answer, you can use wildcards to specify patterns of files and directories to list. Here are some examples:
- List all files and directories in the current directory that start with the letter “a”:
ls a*
- List all files and directories in the current directory that have a “.txt” file extension:
ls *.txt
- List all files and directories in the current directory that start with “a” and have a “.txt” file extension:
ls a*.txt
3. Displaying file sizes with “ls -l”: When using the “ls -l” command, you’ll see the file size listed in bytes. However, you can use the “-h” option to display file sizes in a more human-readable format, such as “1.2K” or “5.8M”.
4. Using “ls” with colors: By default, the “ls” command does not use any colors to highlight files and directories. However, you can use the “ — color” option to enable colorized output. This can make it easier to distinguish between different types of files and directories. Here’s an example:
ls --color
This will display directories in blue, executables in green, symbolic links in cyan, and so on.
5. Displaying files in a tree structure with “ls”: You can use the “ — tree” or “-T” option with the “ls” command to display files and directories in a tree structure. This can be useful for visualizing the directory structure of your system. Here’s an example:
ls --tree
This will display all files and directories in the current directory and any subdirectories in a tree structure.
I hope you find this additional information helpful!
References
Sure, here are some references for the additional information I provided:
- Displaying permissions and ownership with “ls -l”:
- Linux File Permissions Explained. (n.d.). Retrieved March 23, 2023, from https://www.linux.com/training-tutorials/linux-file-permissions-explained/
2. Using “ls” with wildcards:
- How to Use Wildcards in Linux (with Examples). (2022, January 31). Retrieved March 23, 2023, from https://www.hostinger.com/tutorials/linux-wildcards/
3. Displaying file sizes with “ls -l”:
- Linux ls command. (n.d.). Retrieved March 23, 2023, from https://www.computerhope
4. Displaying permissions and ownership with “ls -l”:
- Linux File Permissions Explained. (n.d.). Retrieved March 23, 2023, from https://www.linux.com/training-tutorials/linux-file-permissions-explained/
5. Using “ls” with wildcards:
- How to Use Wildcards in Linux (with Examples). (2022, January 31). Retrieved March 23, 2023, from https://www.hostinger.com/tutorials/linux-wildcards/
6. Displaying file sizes with “ls -l”:
- Linux ls command. (n.d.). Retrieved March 23, 2023, from https://www.computerhope.com/unix/uls.htm
7. Using “ls” with colors:
- Linux ls command. (n.d.). Retrieved March 23, 2023, from https://www.computerhope.com/unix/uls.htm
8. Displaying files in a tree structure with “ls”:
- Linux ls command. (n.d.). Retrieved March 23, 2023, from https://www.computerhope.com/unix/uls.htm
9. Ubuntu Manpage: ls(1) — list directory contents. (n.d.). Retrieved March 23, 2023, from https://manpages.ubuntu.com/manpages/bionic/man1/ls.1.html
10. How to use the ls command in Linux. (2021, April 14). Retrieved March 23, 2023, from https://www.networkworld.com/article/3539429/how-to-use-the-ls-command-in-linux.html