How to Use the Command 'exa' (with examples)
exa
is a modern replacement for the traditional ls
command, used for listing directory contents in Unix-based systems. Built with usability in mind, exa
adds multiple useful features such as improved output formatting, a tree view mode, built-in Git integration, and file size and modification date sorting. It enhances the user experience by providing colorful and comprehensive information about your files and directories, making it a powerful tool for users who navigate file systems frequently.
Use case 1: List Files One per Line
Code:
exa --oneline
Motivation:
Displaying files one per line is particularly useful when you have a directory with numerous files and you want to quickly scan through the list without the distraction of additional information, such as file sizes or permissions. This view minimizes clutter and allows you to focus solely on the file names.
Explanation:
--oneline
: This argument arranges the output so that each file name is printed on a separate line. No additional details are provided, making it a concise way to view file names.
Example Output:
file1.txt
file2.txt
image.jpg
document.pdf
Use case 2: List All Files, Including Hidden Files
Code:
exa --all
Motivation:
In many scenarios, especially troubleshooting or configuration adjustment, hidden files (those starting with a dot) are critical. These files often contain vital configuration settings or data that might be the root of an issue or require updates.
Explanation:
--all
: This flag ensures that all files, including those that are hidden (beginning with a ‘.’ in their names), are listed in the output. It’s particularly useful for a comprehensive overview of directory contents.
Example Output:
file1.txt
file2.txt
.hiddenfile
.config
Use case 3: Long Format List of All Files
Code:
exa --long --all
Motivation:
When you need detailed information about files, such as permissions, ownership, size, and last modification date, the long format list is indispensable. This is particularly useful for system administrators who need to manage permissions or assess the state of files.
Explanation:
--long
: This option provides a detailed view with columns for permissions, number of links, owner, group, size, date of last modification, and name.--all
: As described above, ensures that even hidden files are displayed.
Example Output:
.rw-r--r-- 1 user group 1.2k Jan 10 10:00 file1.txt
.rw-r--r-- 1 user group 3.4k Jan 12 12:02 file2.txt
.rw-r--r-- 1 user group 678 Jan 11 11:01 .hiddenfile
Use case 4: List Files with the Largest at the Top
Code:
exa --reverse --sort=size
Motivation:
When trying to clean up disk space, identifying the largest files in a directory can be extremely helpful. By sorting files by size, you can prioritize which files to delete or move.
Explanation:
--reverse
: This argument reverses the order of the sort, which by default is ascending. Combined with--sort=size
, it displays files from largest to smallest.--sort=size
: This command sorts the files based on their size.
Example Output:
.rw-r--r-- 1 user group 5.0M Jan 13 13:03 bigfile.dat
.rw-r--r-- 1 user group 3.4k Jan 12 12:02 medium.doc
.rw-r--r-- 1 user group 1.2k Jan 10 10:00 small.txt
Use case 5: Display a Tree of Files, Three Levels Deep
Code:
exa --long --tree --level=3
Motivation:
Understanding the hierarchy of files and directories is vital, especially in large projects or complex file structures such as web applications or system directories. A tree view provides an intuitive representation.
Explanation:
--long
: Again, this option shows detailed information about files.--tree
: This argument displays the files and directories in a tree-like format, showing the structure and hierarchy.--level=3
: This limits the display depth to three levels, which helps manage the complexity while still providing detailed information.
Example Output:
.
├── file1.txt
├── dir1
│ ├── file2.txt
│ └── dir2
│ └── file3.txt
└── dir3
├── file4.txt
└── dir4
└── file5.txt
Use case 6: List Files Sorted by Modification Date (Oldest First)
Code:
exa --long --sort=modified
Motivation:
Sorting files by their last modification date is crucial for tasks like backup management or finding recently edited files for further work. Viewing the oldest updated files first might help in troubleshooting processes by looking at files that haven’t been altered recently.
Explanation:
--long
: Displays the details of files in the directory.--sort=modified
: This sorts the output based on the last modification time, showing the oldest files at the top.
Example Output:
.rw-r--r-- 1 user group 1.2k Jan 10 10:00 oldfile.txt
.rw-r--r-- 1 user group 3.4k Jan 12 12:02 newerfile.txt
.rw-r--r-- 1 user group 5.0M Jan 13 13:03 latestfile.doc
Use case 7: List Files with Their Headers, Icons, and Git Statuses
Code:
exa --long --header --icons --git
Motivation:
For developers working within Git repositories, it is useful to see the Git status of files directly in the directory listing. Adding headers and icons can improve readability and help users quickly identify file types.
Explanation:
--long
: Shows detailed file information.--header
: Adds headers to the columns, making it easier to understand what each column represents.--icons
: Displays file type icons next to file names for quick visual identification.--git
: Shows the Git status of each file, such as if it has been modified, indexed, or is untracked.
Example Output:
Permissions Size User Group Date Modified Git Status Filename
.rw-r--r-- 1.2k user group Jan 10 10:00 M file1.txt
.rw-r--r-- 3.4k user group Jan 12 12:02 ?? newfile.sh
.rw-r--r-- 5.0M user group Jan 13 13:03 file2.py
Use case 8: Don’t List Files Mentioned in .gitignore
Code:
exa --git-ignore
Motivation:
When working in a Git-based project, ignoring files specified in the .gitignore
can be helpful for reducing clutter. This ensures the focus remains solely on the relevant files.
Explanation:
--git-ignore
: This option excludes any files or directories specified in the.gitignore
file from the output list, making it easier to concentrate on important files that are being versioned.
Example Output:
file1.txt
dir
file2.txt
Conclusion:
The exa
command, with its modern and intuitive options, provides functionalities that enhance traditional file listing by making output more readable and informative. Its versatility, shown through these use cases, caters to various user needs such as listing hidden files, sorting by different criteria, visualizing file hierarchies, and focusing on version-controlled content within directories. The command, therefore, stands out as a valuable tool for efficient file system navigation and management.