Hello, friends and welcome to the complete Linux command line tutorial for intermediate users.
I have already written a tutorial on basic Linux commands for beginners If you’re are completely new to Linux then I suggest you check that tutorial first.
I use many of the commands in this tutorial that I have discussed in the earlier tutorial. So be sure that you know all the basic stuff before continuing with this article to avoid frustration.
I urge you to type and tweak around every command that I’m going to mention in the post because the practice is the key.
So let’s start.
Linux Command Line Tutorial For Intermediate Users
This command is used to print something on the terminal
For eg: echo “hello world”
Difference between “ “ (double quotes), ‘ ‘ (single quotes),
The above command can also be run without using the quotes, like echo hello world
The thing is that all the extra whitespace will be neglected if you run the echo command without using the quotes. To understand this try running the exact following command:
echo hello world
You will see that all the extra whitespaces are neglected and output comes out to be the just: hello world
Now you may be thinking then what the use of the quotes. Just try to run the exact command that you just ran using double quotes.
echo” hello world”
In the above command, all the extra whitespaces will be preserved and printed in the terminal/console.
One more thing is that if you use variable between the double quotes or without quotes the value of the variable will be printed.
On the contrary, the single quotes ‘ ‘ are used when you simply want to print whatever is between the quotes as it is. It doesn’t matter, even if we try to use a variable inside single quotes it will be printed as it is.
Just try running the following commands you will understand the difference more clearly
yeah! You got the point
I think you may have guessed what this command does! If not, then follow along.
This command is simply used to print the sorted lines from a file.
So suppose you have a file test.txt which contains some random data and you want to sort the data on the file and print it on the terminal.
You can use the command sort test.txt. This command by default sorts the file in alphabetical order and print the result in the terminal.
To do a reverse sort of alphabets just use the r with sort -r test.txt
There are many other switches/options that you can use with the sort command.
To look at what other switches are available just run man sort command. This will open the manual page for the sort command and you can do this with any command of your choice.
You can search the manual for possible commands like this: man -k <command-name>.
Head and tail
The head command is used to extract a certain number of lines from a text file.
So let’s try an example
Make a file containing ten or more than ten line. Or you just run the following command to create a file.
seq 1 15 > demo.txt
Now open demo.txt and you can see that it contains numbers ranging from 1 to 15.
Now suppose we only want to print above four lines from the file demo.txt. we will use the following command:
head -n4 demo.txt
The tail command is just the opposite of the head command. To extract four lines from the bottom of the file we will use:
tail -n4 demo.txt
By default, the output of a command is displayed in the terminal. But what if we want to store that data into a file?
One way could be that you copy all the data that is displayed in the terminal and paste it into a file and save it. But that doesn’t seem productive. The best way is to use the output redirection operator.
The greater than symbol > is known as output redirection operator and is used to redirect output to a file instead of the terminal.
Let’s look an example: suppose we want to store the output of the ls command in a file instead of getting the result in the terminal. We can do something like this:
ls > dirlisting.txt
Here’s is another example:
try running cat > hello.txt
whoa! Did nothing happen?
But the prompt got vanished!
Yeah, this is because now you have entered into the file named hello.txt
The cat command just created the file hello.txt and opened it automatically for you to enter some data into it.
Now type anything you want and when you’re done with inputting data hit Ctrl-D this will take you out of the file. Now, look at the content of the file hello.txt
You can append the content of the file using >>
For eg: cat >> hello.txt
If you simply do cat > hello.txt then put some content into it all the previous content will be overridden.
Now let’s talk about the input redirection operator. The input redirection operator is denoted by less than symbol < we just created a file in which we stored the listing of the directory.
Now we can supply that file as an input to the ls -l command to get the long listing of all the directories stored in that file.
See, how it’s done.
This piping helps to pass the output of a command as an input to the other command. This is denoted by the vertical pipe symbol |
Piping combines the functionality of the input and the output redirection operator. You can use any many pipe operations as you want in a single command.
For eg: seq 1 5 | head -n2
if you finding yourself running long commands over and over again then you can use create an alias for that command. This is just like creating a shortcut.
So suppose, for some reason you want to run the following command over and over again:
It will be a headache to write this command every time you want to run it.
So what you can do is that you can create a shortcut for this command like this:
alias change= ‘cd ~/Documents/newFolder0/newFolder1/newFolder2/newFolder3’
Now every time you type change and hit enter the command in the alias will be run and you will get your desired result.
Using aliases can save you a lot of time and hence you’ll be able to do your work more quickly and efficiently.
You can also give multiple commands to an alias. You just need to separate each command with the semicolon ; Like this:
create= cd ~/Documents/newFolder0/newFolder1/newFolder2/newFolder3; echo “hello world” > hello.txt
To remove an alias you can use the command alias
For eg: unalias create
To remove all the alias using the following command: unalias –a
To do that follow the steps given below:
Step1: Type nano ~/.bashrc on the command line. The .bashrc file will be opened.
Step2: Press CTRL+W. This will open search function of the nano editor.
Step3: Type aliases in the search and hit enter.
step4: Add the desired alias as shown in the image below.
Step 5: Press CTRL+X then enter Y to save the changes made in the file.
Suppose you want to bundle a number of files together you can the command:
tar -cf <archive-name>.tar someFile.txt anotherFile.txt
So let’s discuss the switches/flags used with the tar command in order for it works.
c stands for create, and is used every time you want to create a new tar archive.
f stands for file and is used to specify the file to be used.
x (extract) Extract files from the archive.
For eg: tar -xf <file-name>. This will extract the specified tar archive file.
t (table) Display the table of contents (list).
Another one: tar -tf <file-name>. This will list the files in the specified tar archive file.
We have discussed tar now let’s look at the compression utility provided by Linux, gzip.
If you want to save space on your disk you must consider compressing the files. The gzip utility will help you do the same.
For eg: to compress a file use: gzip <file-name>
And to uncompress use gunzip <file>.gz
Wildcards are some special character which has a special meaning in the Linux.
The wildcard can be used with almost all of the command. You will find it very handy using a wildcard. It makes you more productive and saves you lot of time.
Following are the wildcards used in the Linux:
* – matches zero or more characters.
For eg: ls a*.txt
? – matches exactly one character.
For eg: ls a?.txt
[ ] – A character class. Matches any of the characters included between the brackets. Matches exactly one character.
For eg: ls [abdn].txt
[!] – Matches any of the characters NOT included between the brackets. Matches exactly one character.
For eg: ls [!abdn]*.txt
Tip: you can also give ranges like [a-z,0-9]
File permissions are represented in “trwxrwxrwx” format where “t” tells about the type of the file like a directory or a simple file.
“r” stands for read permission, “w” stands for write permission “x” stands for executable permission. These are the different permissions which can be assigned to a user.
The first set assigned, tells the permissions given to the administrator, the second set tells the permission given to the group of the admin and the third set tell about the permissions given to the rest of the people.
A dash “-” in between represents that the user do not have that permission. For instance drwxrw-r–.
In order to change the permissions of a file, we use mostly use chmod command. Syntax for the same is :
chmod <file-permission> file-name
Different file permissions are assigned a binary digit. Like “read” is assigned digit 4, “write” is 2 and “execute” is 1.
Now look at this permission rw-r-r– , these permissions can be written as 644.
we can simply use the command chdmod 664 a.txt for changing the permission of a file.
The du command stands for disk usage. This command is used to check the disk usage of a directory or file.
To check the disk usage of a file simply type du <file-name> and hit enter.
Example: du etc
Keep in mind that the size displayed is in bytes. To display the size in megabytes use the switch -h, which stand for human-readable form.
Example: du -h etc
In a situation when you can’t figure out the type of a file, whether it is a directory, simple text file or other types. You just need to run: file <file-name> and hit enter. It will display you the type of the file.
Rundown: Linux Command Line Tutorial For Intermediate Users
So these were some commands that you can use to perform your day to day task seamlessly.
These are only the fraction of commands that I covered. But, the Linux Operating system offers much more powerful commands than these.
Of course, I can not cover all of them here but I encourage you to use search utility to find the commands that fulfill your needs.
In the next section of the Linux Command Line Tutorial, I’ll be covering some advanced Linux commands that will really make you feel the power of Linux.
Commands that are used for system administration, process management, grep utility and much more. So stay tuned and subscribe to the email list to get informed as soon as I make the post live.
If you enjoyed the tutorial spread the word by sharing it with like-minded people and as always if you have any query, question, suggestion feel free to drop a comment below and I will get back to you ASAP.