Linux

10 Most Common Linux Commands – And How to Use Them

Linux knowledge is vital no matter what your chosen IT career is. We covered some of the reasons you need to learn Linux in another blog post.

There is no need to be an expert, of course. Just navigating the Linux command line and carrying out a few simple tasks will suffice. If you want to get certified, then I strongly recommend taking the Linux LPI Essentials certification.

In this post I’ll list what I consider to be the 10 must-know commands along with examples of how they are used. This is just a kicking off point of course and not designed to help you manage a Linux network or server.

ls – list the contents of a directory

To use the ls command in Linux, open a terminal window and type ls followed by any options or arguments you want to use. For example, to list the files and directories in the current directory, you would type ls and press enter.

To list the files in a specific directory, you can provide the directory path as an argument to the ls command. For example, to list the files in the /home/user/documents directory, you would type ls /home/user/documents and press enter.

Additional options can be added to the ls command to modify its behavior. For example, the -l option displays the files in a long format, including file permissions, ownership, and timestamp. To display the files in the current directory in long format, you would type ls -l and press enter.

cd – change the current working directory

The cd (change directory) command is used in Linux to navigate between directories in the file system.

To use the cd command, open a terminal window and type cd followed by the path of the directory you want to navigate to. For example, to navigate to the /home/user/documents directory, you would type cd /home/user/documents and press enter.

You can also use cd command without any argument to navigate to your home directory. For example, to navigate to your home directory, you would type cd and press enter.

If you want to navigate to the parent directory of the current directory, you can use cd .. . For example, if you’re currently in the /home/user/documents directory, you can navigate to the parent directory (i.e. /home/user) by typing cd .. and press enter.

Additionally, you can use cd - command to navigate to the previous directory you were in.

It’s worth mentioning that you can use the TAB key to auto-complete directories and file names.

cd command

pwd – print the current working directory

The pwd (print working directory) command is used in Linux to display the current directory path.

To use the pwd command, open a terminal window and type pwd and press enter. This will display the full path of the current working directory you are in. For example, if you are currently in the /home/user/documents directory, running the pwd command will display /home/user/documents in the terminal.

You can use pwd command in any directory you want, it will always show the current directory you are in. It doesn’t take any arguments or options.

You can also use it in combination with other commands, for example, when you want to know in which directory you’re currently working and use that path as an argument to another command.

pwd command

mkdir – create a new directory

The mkdir (make directory) command is used in Linux to create new directories.

To use the mkdir command, open a terminal window and type mkdir followed by the name of the directory you want to create. For example, to create a new directory called myfolder, you would type mkdir myfolder and press enter. This will create a new directory named myfolder in the current working directory.

You can also specify a path to create a directory in a specific location. For example, to create a directory called myfolder in the /home/user/documents directory, you would type mkdir /home/user/documents/myfolder and press enter.

You can also create multiple directories at once by separating their names with a space, for example mkdir folder1 folder2 folder3 will create three directories.

You can also use the -p option to create parent directories if they do not exist, for example, mkdir -p /home/user/documents/myfolder/myfiles will create all the parent directories if they don’t exist.

It’s worth noting that if you try to create a directory that already exists, mkdir will return an error message unless you use the -p option, in that case it will just skip creating the existing directories.

mkdir command

rm – remove a file or directory

The rm (remove) command is used in Linux to delete files and directories.

To use the rm command, open a terminal window and type rm followed by the name of the file or directory you want to delete. For example, to delete a file called myfile.txt in the current working directory, you would type rm myfile.txt and press enter.

You can also specify a path to delete a file or directory in a specific location. For example, to delete a directory called myfolder in the /home/user/documents directory, you would type rm -r /home/user/documents/myfolder and press enter. The -r option is used to remove the directory and its contents recursively.

It’s worth noting that the rm command is a dangerous command, because once a file or directory is deleted with rm, it cannot be recovered easily.

For this reason, it’s recommended to use the -i option which prompts you for confirmation before deleting each file. For example, rm -i myfile.txt will ask you to confirm before deleting the file.

Another option you can use is -f option, it will delete the files and directories without any confirmation. It’s not recommended to use this option, as it can lead to the accidental deletion of important files.

It’s also important to note that when you delete a directory using rm -r command, it will delete all of its content recursively, which means all the files and subdirectories inside that directory will be deleted as well.

rm command

cp – copy a file or directory

The cp (copy) command is used in Linux to copy files and directories.

To use the cp command, open a terminal window and type cp followed by the source file or directory and the destination. For example, to copy a file called myfile.txt from the current working directory to a directory called myfolder, you would type cp myfile.txt myfolder/ and press enter.

You can also specify a path for both the source file or directory and the destination. For example, to copy a directory called myfolder from the /home/user/documents directory to the /home/user/backup directory, you would type cp -r /home/user/documents/myfolder /home/user/backup/ and press enter. The -r option is used to copy the directory and its contents recursively.

Additionally, you can use the -p option to preserve the file attributes like ownership, permissions and timestamps. The -v option can also be used to display verbose output while copying.

It’s worth noting that if you try to copy a file or directory to a location where a file or directory with the same name already exist, the cp command will overwrite it without any warning unless you use the -n option that will not overwrite existing files.

Also, if you copy a directory using cp -r command, it will copy the directory and all of its contents recursively, it means all the files and subdirectories inside that directory will be copied as well.

cp coommand

mv – move or rename a file or directory

The mv (move) command is used in Linux to move or rename files and directories.

To use the mv command to move a file, open a terminal window, and type mv followed by the source file and the destination. For example, to move a file called myfile.txt from the current working directory to a directory called myfolder, you would type mv myfile.txt myfolder/ and press enter. This will move the file myfile.txt from the current directory to the myfolder directory.

To use the mv command to rename a file, open a terminal window and type mv followed by the source file and the new name. For example, to rename a file called myfile.txt to newfile.txt, you would type mv myfile.txt newfile.txt and press enter.

You can also specify a path for both the source file or directory and the destination. For example, to move a directory called myfolder from the /home/user/documents directory to the /home/user/backup directory, you would type mv /home/user/documents/myfolder /home/user/backup/ and press enter.

It’s worth noting that if you try to move a file or directory to a location where a file or directory with the same name already exists, the mv command will overwrite it without any warning unless you use the -n option that will not overwrite existing files.

Also, when you move a directory with mv command, the directory and all its contents are moved to the new location. The original directory and its contents no longer exist in the original location after the move.

mv command

nano – open a text editor

nano is a text editor in Linux that is commonly used in the command line interface. It is known for its user-friendly interface and is a good option for beginners.

To use nano to create or edit a file, open a terminal window and type nano followed by the name of the file you want to create or edit. For example, to create a new file called myfile.txt and edit it with nano, you would type nano myfile.txt and press enter. This will open the nano text editor with an empty file called myfile.txt.

You can also specify a path to open a file in a specific location. For example, to edit a file called myfile.txt in the /home/user/documents directory, you would type nano /home/user/documents/myfile.txt and press enter.

Once you are in the nano editor, you can use the arrow keys to navigate and make changes to the file. To save your changes, press CTRL + O and then press ENTER to confirm the file name. To exit nano, press CTRL + X.

nano has a built-in help system that lists the available commands and their shortcuts, you can access it by pressing CTRL + G.

It’s worth mentioning that nano is not the only text editor you can use in the command line interface, other popular alternatives are vi, vim, emacs, gedit, among others.

nano
You will then enter the text editor.
nano command

apt-get – install or remove software packages

apt-get is a command-line utility for managing packages in Linux distributions that use the Debian package management system, such as Ubuntu, Debian, and Linux Mint.

The basic syntax for using apt-get is apt-get [options] [command] [package(s)].

Here are a few examples of how to use apt-get:

  • To update the package list and upgrade all installed packages to their latest version, you would type sudo apt-get update && sudo apt-get upgrade and press enter.
  • To install a new package, you would type sudo apt-get install [package_name] and press enter. For example, to install the package htop, you would type sudo apt-get install htop and press enter.
  • To remove a package, you would type sudo apt-get remove [package_name] and press enter. For example, to remove the package htop, you would type sudo apt-get remove htop and press enter.
  • To search for a package, you would type apt-cache search [package_name] and press enter. For example, to search for a package called htop, you would type apt-cache search htop and press enter.
  • To show package information, you would type apt-cache show [package_name] and press enter. For example, to show information about package htop, you would type apt-cache show htop and press enter.

It’s worth noting that apt-get commands need to be run with superuser privileges, that’s why it’s common to see sudo before the command, it’s used to run the command as the root user.

man – view the manual for a command

The man (manual) command is used in Linux to display the manual pages for a command. Manual pages provide detailed information about the usage, options, and arguments of a command.

To use the man command, open a terminal window and type man followed by the command for which you want to view the manual page. For example, to view the manual page for the ls command, you would type man ls and press enter. This will display the manual page for the ls command in the terminal.

You can scroll through the manual page using the arrow keys or the Page Up and Page Down keys. To exit the manual page and return to the command prompt, press q.

You can also use the -k option to search for a keyword in the manual pages. For example, to search for all manual pages containing the keyword “copy”, you would type man -k copy and press enter.

You can also use the -f option to display the manual page for a command without specifying the command name, it will show the command with the given name. For example, to display the manual page for the command that is used to copy files, you would type man -f cp and press enter.

It’s worth noting that man command shows the manual pages for the command that are installed on your system, so you might not find some commands that you’re looking for if they’re not installed.

linux plus new

101 Labs Newsletter

Exam