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.

Start the photo gallery(13 pictures)

A comparison of the top 12 of the currently most popular Linux distributions

List hidden files and directories

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

With “ls -a” the terminal also shows hidden folders and files. Image: GIGA

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

The command
The command “ls -A” hides the first two entries (.) And (..) in contrast to “ls -a”. Image: GIGA

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

The command
The command “ls -r” outputs the list in reverse alphabetical order. Image: GIGA

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).

With
With “-t” the most recent data is displayed at the top. “-Tr” shows the oldest data at the top. Image: GIGA

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

The
The “ls -l” command shows a lot more information. Image: GIGA

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

The addition
The addition “-h” outputs file sizes that people can also relate to. Image: GIGA

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

With
With “-s” the file size is also displayed. Image: GIGA

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

This handy addition lists the folders first.  Image: GIGA
This handy addition lists the folders first. Image: GIGA

List subdirectories recursively

The “ls” command can also display the contents of subdirectories. Simply use the command line option “-R”.

ls -R

The command also lists the contents of sub-folders.  Image: GIGA
The command also lists the contents of sub-folders. Image: GIGA

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.

On the left the standard colors of the terminal from Linux Mint.  On the right the Monkai theme.  Image: GIGA
On the left the standard colors of the terminal from Linux Mint. On the right the Monkai theme. Image: GIGA

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.

The Monokai theme for the terminal has been installed.  Image: GIGA
The Monokai theme for the terminal has been installed. Image: GIGA

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 “Profile“You click on the plus symbol (+) and enter a name for the new profile, for example “Standard“, 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.

So you can restore the default theme.  Image: GIGA
So you can restore the default theme. Image: GIGA

“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:

This is what my terminal outputs when I type in
This is what my terminal outputs when I type in “ls”. Image: GIGA

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}}“

Here you insert this line so that
Here you insert this line so that “exa” will work later. Image: GIGA

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‘

This block should look like this for you.  Image: GIGA
This block should look like this for you. Image: GIGA

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.

Which operating system do you like best? (Desktop)

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).



Reference-www.giga.de