Top best commands in Linux, most useful with instruction

Top best commands in Linux, most useful with instruction

· 5 min read
Top best commands in Linux, most useful with instruction
Top best commands in Linux, most useful with instruction 

1. ls command: List the contents of a folder

This is among the first few commands a new Linux user learns. This command lets you see what files and folders are in your current folder.

ls

You can use the long listing option ls -l to see details like file size, permission, modified time, etc. You can sort and control these options if you want to.

ls -l
ls command ubuntu

Related Read: ls command examples

2. cd command: Change the directory

By default, you start in your home directory. You’ll often require to change the directory and move to another one.

For example, you downloaded a deb file or script. Now you want to run it. You can do that from your present working directory by providing the full path but switching to that location makes things easier.

The cd command stands for change directory; with this, you can change your location and move to another directory.

cd command examples
cd command examples

At this point, I highly recommend reading about the concept of paths in Linux so that things are easy to understand while navigating through directories in the Linux command line.

Recommended Read: cd command examples

3. cat command: Read a text file

If you quickly want to see the contents of a text file in Linux, cat is the command you use. It displays the contents on the screen.

cat filename
cat command example
Cat command example

You can also use the cat command to create new files or add more text to existing files.

Recommended Read: cat command examples

4. less command: Read a large text file

The cat command is good enough for viewing small text files. But I won’t recommend using cat if you have a huge text file with hundreds of lines. It will flood your screen with all the text, and you will have difficulty with it.

This is where the less command comes into the picture. When you open a file with less, it opens the file in pages. You can scroll up/down, look for text, and more.

reading large files with less command
Reading large files with less command

Once you are done reading the file, you can exit the less view by pressing the Q key. You’ll notice that nothing is displayed on the screen. Your screen is clean.

Suggested Read: less command examples

5. touch command: Create new files

There are multiple ways of creating new files in the Linux terminal. The cat command you saw above can also create new files.

However, I prefer the touch command for this purpose.

touch new_file_name
touch command ubuntu
Touch command example

If you use it with existing files, their timestamps will be modified.

Also Read: touch command examples

6. mkdir command: Make new folders

While there is no specific command for creating new files, there is a dedicated command for making new folders (or directories, as we call them in Linux).

mkdir new_dir
mkdir command example
mkdir command example

Explore More Here: mkdir command examples

7. cp command: Copy files and folders

Copying files and folders in the command line is also one of the common tasks you will encounter. The cp command, short for copy, is used for this purpose.

Imagine that you have to modify a configuration file. A smart move will be to copy the file with another name. This way, you’ll have a backup of the file.

cp existing_file.txt existing_file.back

You can use the same cp command for copying directories as well. For that, you must specify the recursive option -r:

cp -r dir another_location
cp command example
cp command example

You May Also Read: cp command examples

8. mv command: Cut-paste or rename files and folders

The mv command stands for ‘move’. When you copy a file to another location, it remains in its original place.

The mv command moves the files and folders to the other location. You can think of it as a cut-paste operation.

mv file.txt /another/location

You can use the mv command to rename the file as well.

mv file.txt new_file.txt

The same mv command also moves or renames folders without any special options.

mv command example
mv command examples

Recommended Read: mv command examples

9. rm command: Remove files and folders

You use the rm (short for remove) command to delete files in the Linux terminal.

rm filename

There is no undo option after you delete files in the command line. This is why you should be extremely careful while deleting files. If you are afraid of deleting the wrong file, use the interactive mode with option -i, which gives you an additional prompt to confirm the action.

rm -i filename

With the recursive option -r, you can also use the same rm command to delete folders.

rm command examples
rm command examples

Recommended Read: rm command examples

10. nano: Edit files

Sooner or later, you’ll be required to make changes to the contents of a file. Imagine that you have to change a configuration file of SSH, grub, or some other application.

There are command line-based text editors for this purpose. Ubuntu comes with Nano editor preinstalled, and it is relatively easier to use than Vim, Emacs, etc.

If you are curious about differences, read our Nano vs. Vim comparison article.

Easier to use doesn’t mean the same comfort as a GUI-based text editor. You will have to use the keyboard shortcuts for moving around, making changes, saving, and exiting files.

To open a new, unnamed file with nano, use:

nano

To edit an existing file in Nano, use:

nano filename

In both cases, you should see an interface like this.

nano command example
Nano command example

To save (or discard changes) and exit the editor interface, use the Ctrl+x keys.

Please refer to the Nano beginner guide I created earlier to get comfortable with it.