12 tips to master the LS command in Linux
In the terminal, “ls” is one of the most important commands for listing folder and file names. The LS command can still be improved a lot. How you can get more color and power in “ls”, we show you here for Ubuntu-based Linux distributions such as Ubuntu, Linux Mint and Pop! _OS.
The first tips show what you can do with the ls command. The last two tips show you how to make the ls command more colorful.
Contents
- 1 List hidden files and directories
- 2 Entries “. “And” .. “do not display
- 3 Show files and directories in reverse order
- 4 Show output sorted by date
- 5 View files / folders with details
- 6 Show file sizes in megabytes instead of bytes
- 7 Show only file names with file sizes
- 8 List directories first
- 9 List subdirectories recursively
- 10 What else is possible with “ls”?
- 11 Nicer colors for “ls”
- 12 “Exa” is the better “ls”
- 13 Which operating system do you like best? (Desktop)
The terminal command “ls” does not display any hidden files or directories by default. However, you can change that by adding the command line option “-a” (all) at the end.
ls -a
Entries “. “And” .. “do not display
The command “ls -a” also shows the entries for the current and previous directory at the top of the list, which are marked with a period (.) And two consecutive periods (..). If you want to save yourself the display of these entries, simply use the command line option “-A” (almost-all).
ls -A
Show files and directories in reverse order
By default, “ls” displays files and directories in alphabetical order. You can display it in reverse order with the command line option “-r” (reverse).
ls -r
Show output sorted by date
The “ls” command lists files and directories sorted by date using the “-t” (time) command line option. If you want to have the elements with the most recent date listed below, use “-tr” (time, reverse).
View files / folders with details
The “-l” (long) command line option lists files and directories with permissions, owners, file size, date, and file name.
ls -l
Show file sizes in megabytes instead of bytes
By default, the “ls -l” command displays file sizes in bytes. But if you prefer to view these in megabytes, gigabytes, etc., you can also use the command line option “-h” (human-readable).
ls -lh
Show only file names with file sizes
While “ls” only shows file / directory names and “ls -l” a lot of information with file size, you can only list the names with file size. To do this, use the command line option “-s” (size). We also recommend “-h” (human readable).
ls -sh
List directories first
The terminal command “ls” lists folders and directories mixed in alphabetical order. If you want to display the folders first, use the command line option “–group-directories-first”.
ls --group-directories-first
List subdirectories recursively
The “ls” command can also display the contents of subdirectories. Simply use the command line option “-R”.
ls -R
What else is possible with “ls”?
If you want to see all available command line options of “ls”, type the command in the terminal man ls
one. Scroll down the list to see everything. With the “q” key you close the overview again.
Nicer colors for “ls”
The terminal command “ls” shows some entries in color by default. But there is much more that can be done: We recommend the Monokai theme for the Gnome Terminal. The Gnome Terminal is preinstalled in Ubuntu, Linux Mint and Pop! _OS, for example. One more thing in advance: The theme changes the terminal colors as a whole, not just for the LS command. But that is intentional.
Open the terminal by using the keyboard shortcut [Strg] + [Alt] + [T] presses. For this you need the small tool dconf, which you can install with the following terminal command:
sudo apt install dconf-cli
Enter your password and confirm with [Enter]. The tool may already be preinstalled with you. You also need the tool “git”, which you install as follows:
sudo apt install git
Enter your password and confirm with [Enter]. Then you clone the associated package source (repository) of the Monokai theme with the command:
git clone https://github.com/0xComposure/monokai-gnome-terminal
The folder “monokai-gnome-terminal” with the required content has now been created in your “personal folder”. Now switch to the folder inside the terminal with the following command:
cd monokai-gnome-terminal
Then you install the terminal theme with the command:
./install.sh
You will probably now see the message “No profile found“. Confirm the request by “yes“And confirm with Enter to create a new profile in the terminal. Linux now asks which terminal profile should be selected. Select the number here “Monokai“And confirm with [Enter]. Linux asks if you are sure to overwrite the selected profile. Confirmed again with “yes” and [Enter]. Closes the terminal. In your “personal folder” you can now delete the “monokai-gnome-terminal” folder because it is no longer needed. Then open a new terminal to see it in the colors of the Monokai theme.
Back to the old terminal theme
If you want to undo the changes, click the right click in a terminal window and select “ideas” out. Next “Profiles“You click on the plus symbol (+) and enter a name for the new profile, for example “default“, And confirmed with [Enter]. The new profile contains the standard colors. Click on the new profile “Standard” and then next to the text “Standard” on the small arrow pointing downwards. Elects “Set as default” out. Close the window.
“Exa” is the better “ls”
I don’t feel like giving command line options all the time when listing files / directories. I also want an even more colorful display of the items listed. For example, when I enter the “ls” command in my terminal, the output automatically looks like this:
To get this view, use the command line program “exa” and create an “alias” for “ls” including the desired command line options. And this is how it works:
First install the package manager “cargo”, which can then be used to install “exa”. The associated command is:
sudo apt install cargo
Then you install “exa” with the command:
cargo install exa
Now you open your home directory with a file manager. Show you all the hidden files. This usually works with the key combination: [Strg] + [H]. Now creates a backup copy of the file “.bashrc“.
Opens the original file “.bashrc“With a text editor and inserts the following line as shown in the screenshot above:
PATH = “$ HOME / .cargo / bin $ {PATH: +: $ {PATH}}”
Look further down in the file for the line that starts with “alias ls“Begins. Put a hash symbol (#) in front of this line to comment it out. Add the following line above:
alias ls = “exa -l –color = always –group-directories-first ‘
Saves the file and closes all open terminals. Opens a new terminal. If you now enter “ls”, you should see the view as I showed it above. Especially in combination with the Monokai theme, the terminal and the LS command now look really good.
You don’t want to miss any news about technology, games and pop culture? No current tests and guides? Then follow us on Facebook (GIGA Tech,
GIGA Games) or Twitter (GIGA Tech,
GIGA Games).