Practice of Common Parameters for the ls Command in Linux/Unix

The ls command is one of the most commonly used commands in Linux and Unix systems. It is used to list the contents of directories. Here are some common parameters and practical examples of using ls in Linux/Unix:

Basic Usage

ls

Lists the files in the current directory.

1. List All Files, Including Hidden Ones

ls -a

Lists all files, including those starting with a dot (hidden files).

2. List Files with Detailed Information

ls -l

Lists files with detailed information such as permissions, number of links, owner, group, size, and modification time.

3. List the files and directories in the current directory, along with detailed information, including hidden files

ls -al

The ls -al command is used to list the files and directories in the current directory with detailed information.

4. Sort Files by Modification Time

ls -lt

Lists files sorted by modification time, with the latest files first.

5. Sort Files by Size

ls -S

Lists files sorted by size, with the largest files first.

6. List Files Recursively

ls -R

Lists all files recursively from the current directory and its subdirectories.

7. Human-readable File Sizes

ls -lh

Lists files with sizes in human-readable format (e.g., 1K, 234M, 2G).

8. List Files in Columns

ls -1

Lists files in a single column.

ls -C

Lists files in columns.

9. List Only Directories

ls -d */  # Note the trailing space

Lists only directories in the current directory.

10. List Files in Reverse Order

ls -r

Lists files in reverse order, from Z to A.

11. List Files by Creation Time

ls -c

Lists files by creation time.

12. List Files by Access Time

ls -u

Lists files by last access time.

13. List Files Sorted Alphabetically

ls -U

Lists files unsorted, but in the order they are stored in the directory.

14. Exclude Certain Files/Directories

ls -A

Lists all files except the current directory (.) and the parent directory (..).

ls -X

Lists files in an order that depends on the locale settings.

15. Show Total Disk Usage

ls -sh

Lists files with sizes in human-readable format and shows the total disk usage at the end.

16. Colorize Output

ls --color

Colorizes the output based on file types and attributes.

17. Ignore Backup Files

ls -a --ignore=*.~

Lists all files, excluding backup files ending in ~.

18. Show Only Files Modified Within Last 24 Hours

find . -mtime -1 -type f -exec ls -ld {} \;

This command uses find to locate files modified within the last 24 hours and then lists their details using ls.

19. List Files in a Specific Directory

ls /path/to/directory

Lists files in the specified directory.

20. List Files Matching a Pattern

ls *.txt

Lists all files matching the pattern *.txt.

Conclusion

These examples cover a wide range of use cases for the ls command. Depending on your needs, you can combine multiple options or use additional commands like grep or awk to filter the output further. If you have specific requirements or need more advanced functionality, feel free to ask, and I can provide more tailored examples.

This entry was posted in Ls Command, File & Directory and tagged , , , . Bookmark the permalink.