This article provides an overview of 15 useful basic Linux commands that you should know. It should offer a helping hand to anyone who just started using a terminal.
You can use the ls command to display the list of files and sub-folders present in the current folder.
If the argument of
ls is a file, you will get a single line output, which will be the file name itself. If the argument of
ls is a folder, you will get its files and sub-folders.
There are many parameters, which you can use with the ls command. For example,
ls -l #(will display the listing in full detail such as owner name, file size, date of last modification, and file permission) ls -s #(will display file size in terms of blocks, where 1 block= 512 characters) ls -a #(will show all hidden files. The name of a hidden file starts with a dot) ls -F #(can be used to check whether a file is executable or it is a folder) ls -R #(is used to display the contents of every sub-folder as well, here ‘R’ stands for recursive). ls -r #(will display the names of files and folders in reverse order, the small ‘r’ stands for reverse) ls -d #(can be used to display the information about a directory (folder) itself)
You can check our ls command article, to find more details about the ls command and how to use it to sort file names.
In Linux, each file has Read, Write and Execute permissions. The set of permissions assigned to a file or folder is called mode.
To set or change permisions of a file or folder, you can use the Change Mode (
chmod) command. However, only the superuser or the owner of the file can issue this command.
chmod 550 header
For a complete understanding of what 550 means in the above command, you can check our full-length tutorial on how to change file and folder permissions.
The Make Directory command (
mkdir) is used to create a directory. However, your user id must have write permission within the folder you want to create the new one in.
mkdir examples #(will create a folder ‘examples’ in the current folder) mkdir -p examples/abc/123 #(will create a folder ‘examples’ in the current folder, along with all parent folders if they don’t exist) mkdir -m 750 examples #(will create a folder ‘examples’ in the current folder and will also set permissions on the folder)
You can use the Remove Directory command (
rmdir) to delete a folder. However, for removing a folder the following conditions are needed.
- The folder you want to delete must be empty.
- Your user id must have write and execute permissions for the parent folder.
- It can’t be your working directory.
rmdir examples #(will delete the folder ‘examples’) rmdir -p examples/abc/123 #(will delete all folders in the path if they meet the above conditions)
The Change Directory
cd command is used to move from one folder to another. This command is often needed to go to a particular folder and perform actions like creating or deleting files and folders.
cd /dev #(will move you to ‘dev’ folder)
If you are not sure about your current directory, you can use Print Working Directory –
pwd command. This command will print the current folder you are working in.
The Super User
sucommand allows you to log in as root temporarily.
The difference between
su- is that when you use
suyou will become root but you will be inside your login shell (your user’s home directory from which you issued the command).
su- makes you become root with root’s login shell (root’s home directory).
This command is used to search a file or folder.
locate finger #(will search the file ‘finger’ and display its path)
The concatenate (
cat) command can be used to perform three functions:
- To display the contents of a file
- To create a text file
- To concatenate two or more files
To display the content of a file, you can write the name of the file with
cat command like this
To create a text file, use
cat command with a greater than sign
cat > prog1
It will open an editor where you can start writing the text. When you finish typing press Ctrl + D to close.
cat prog1 prog2 > prog3
prog 1 and
prog2 files in a new file
This command is used to look at the beginning of a file. It is a useful command to check what is inside a file, but it only shows the first few lines. So, you cannot figure out the length of the file.
By default, you can only see the first ten lines of a file. The good news is that you can change the number of lines to be displayed by specifying a number option:
head -20 <filename>
The reverse of the head command is the
tail. Using the
tail command, you can view the last ten lines of a file. This can be useful for viewing the last 10 lines of a log file for important system messages.
-f parameter, the tail command automatically prints new messages from an open file to the screen in real-time. For example, to actively watch /var/log/messages, type the following as the root user:
tail -f /var/log/messages
This command is used to find specific characters in a file. For example, if you want to find every occurrence of “coffee” in the file sneakers.txt, you would type:
grep coffee sneakers.txt
You would see every line in that file where the word “coffee” is found.
Unless otherwise specified, grep searches are case-sensitive. To allow a case-insensitive search, you can use
-i parameter with grep command.
You can create an empty text file using the touch command.
ps is a very useful command which is used to display all currently executing processes. You can use different parameters with
ps command like
-a (to show currently running processes of all shells)
-au (to show currently running processes of the current user)
-x (to show all processes exclusively)
The details of
ps command can be found in this tutorial.
Once you find the id of a process using
ps command, you can terminate the process using
kill process-id kill -HUP Pid