Most modern Linux distros come with one or more desktop environments already installed. These desktop environments, such as KDE and Gnome are very rich in applications that you can usually get by without using any of the commands or command line utilities directly.
You should still know at least some of the basic Linux commands that you can use from the command line in order to use the system effectively. It not only comes in handy when using a desktop, it is extremely useful while working remotely on another machine.
This is a list of the most basic of the Linux commands that you will mostly use everyday. It covers the basic operation such as creating a folder, changing to a particular folder, viewing the list of files, moving files etc. Each of these commands have a plethora of command line options. We will cover just the basic ones for each of them.
cd (change directory)
The cd command allows you to change your current working directory. Almost all commands have a concept of the working directory where it is executed, which defaults to the user’s current directory when one is not explicitly specified.
When working on a terminal, you are always in a directory whether it be your home directory, root directory or any other. The cd command accepts the directory name as a command line argument, and changes your current working directory. It accepts the argument as either a relative path to the directory or as an absolute path.
bash$ cd /home/lostsaloon/media/
After executing the above command, the current working directory is set to /home/lostsaloon/media. When executed without any argument, the directory is changed to the home directory of the current logged in user.
ls (list contents)
The ls command lists the contents of the specified directory. It accepts the path to a directory (or a file) as a command line argument. You can specify the path as either a relative path or an absolute path. When no argument is specified, it defaults to the current working directory and displays the contents of that directory.
bash$ ls /home/lostsaloon/media
The above command will display the contents, including files and sub-directories in the folder /home/lostsaloon/media. There are several options available to the ls command that allows you to format the output of the command. The most useful ones are -l (long listing format), -i (inode or index number), -s (size of the file) and -a (all files including hidden files).
bash$ ls -lisa ./media
You can also filter and sort the output based on various file metadata using different command line options.
cp (copy file or directory)
The cp command copies a file or a directory from one location to another. It takes at least two arguments, the first is the path to the source or an existing file and the next is the path to the target file location. The basic syntax of the command is as below.
bash$ cp <source file path> <target file path>
If the target location already exists, then the file will be overwritten. You do have the option to override this using the -i (interactive mode), which will prompt you if the destination already exists or the -n or –no-clobber option which will not overwrite the destination. Another common option is the -r or –recursive to copy files recursively from a directory to another directory
mv (move file or directory)
Similar to the cp command above, the mv command will copy the file over to the target or destination and delete the source. This can also be used to rename files. Like the cp commands, it has similar options to overwrite and recursively move files and directories.
bash$ mv <source file path> <target file path>
rm (delete file or directory)
The rm command can be used to remove files and directories. It takes multiple arguments, each of which is the path to the file(s) to be removed. A directory have to be empty in order for it to be removed and directory removal should have the option -r.
bash$ rm <file path> <path to another file> <path to file>
This will remove all the files that are specified as arguments. You can also regular expressions to match multiple files. The command below will remove all files with jpg extension for the img/ folder.
bash$ rm ./img/*.jpg
In order to remove a directory along with all the contents inside it, use the -f or –force option along with the -r or –recursive option, as below. This will remove the folder named backup/ along with all its contents, including files and sub-directories.
bash$ rm -fr ./img/backup/
mkdir (Create a Directory)
If you want to create a new directory, then you should use the mkdir command. It takes multiple arguments each of which is the path to the directory you want to create.
bash$ mkdir <path to directory>/<new directory name>
If you specify paths that are more than one level deep then each of the parent directories should already exist, or else the command will fail. For example, if you use the following command
bash$ mkdir ./img/website/test/jpg
then the folder named img/website/test should already exist. In order to force and create all non-existent parent directories you can use the -p or –parents option as below, which will create directories as needed.
bash$ mkdir -p ./img/website/test/jpg
cat (View file contents)
If you want to view the contents of a file, then you can use the cat command to do so. cat command is usually used to concatenate multiple files to a standard output but it is also a quick and easy way to print files to the console.
bash$ cat <path to file> <path to another file>
There are other commands such as less and more that has better functionality and can be used as well. In order to print the line numbers when the file is displayed , use the -n or –number option.
pwd (Current working directory)
We have mentioned the concept of the current working directory, also called the current directory or just the working directory. You should always be aware of what your working directory is, otherwise your commands will execute differently than you might expect. For example, you could delete the wrong files in the wrong directory.
You can find the current directory with the pwd command. It takes no arguments and just prints out the absolute path of the current directory.
ln (Make links)
One of the cool features in Linux is the concept of links. There are two types of links, namely hard links and soft links. You can create these links using the ln command. Use the following command to create a link called songs to the music/ folder
bash$ ln -sfn music/ songs
The generic format of the command is shown below. The -s option specifies that the link is a symbolic link.
bash$ ln <path to target> <link name>
man (Manual for a command)
One of the most important command is the man command, which displays the manual page for the commands. It is pretty much impossible to know and remember all the various options of a command, especially the rarely used ones. Use the man command to see detailed information of a command and to find the appropriate command line options.
bash$ man <command name>
Another command to display the manual is info command, equivalent to the man command.
bash$ info <command name>
There are usually many common features among the Unix commands. Most times you will find that the command line options are similar or serve a very similar purpose in different commands. For example, the -v option is almost always the verbose option, to print a verbose output. The -i option similarly denote an interactive mode or case-sensitiveness and -f is mostly used to force something like a deletion.
Many of the commands take the file or directory path as one or more of its arguments. You can always specify the path as either relative to the working directory or as an absolute path. Also most commands will accept more than one file path as argument when appropriate, as was shown in rm and cat commands examples. In addition, when specifying the path to the file, you can also use regular expressions to match multiple files and folders.
The above set of commands is the very basic of all the commands but also the most useful and regularly used ones. These should at least get you started and help you perform the basic tasks on the system as you learn more.