A Quick Guide To Get Started With The Linux Command Line
Think working on the command line is complex, old school and obsolete? Think again.
In Linux, the command line is a peerless tool that performs complex tasks with very little effort. For example, try using a GUI (Graphical User Interface) to delete every file with names ending with .tmp and were created within the last 3 months. Then learn how to do the same task through the command line. You will be surprised by the ease and speed with which you can perform this task through the command line, especially if you have to do it regularly.
You can do lots of amazing stuff with commands in Linux and it’s really not difficult to learn. If you are completely new to the Linux command line, you should first get familiarized with CLI (Command Line Interface) navigation, along with some basic file/directory operations. And that’s exactly what we will discuss in this article.
Learn To Navigate Linux Command Line Effectively
Whenever you open a Linux command line shell, you start at a directory (usually your home directory). This is your working directory until you change to some other directory. For users migrating from Windows, a directory in Linux is equivalent to a folder in Windows. Further, a home directory in Linux is conceptually equivalent to a user specific folder — present in C:/Documents and Settings or C:/Users — in Windows. To see the complete path of your working directory, always use the pwd command.
NOTE – The text leading up to and including dollar ($) (or # in some cases) is known as command prompt.
The pwd command outputted /home/himanshu, which means the current working directory is himanshu. Lets understand the output of pwd command in steps :
- / – The beginning forward slash represents the top level directory
- /home – The home directory is a sub-directory under the top level directory
- /home/himanshu – The himanshu directory is a subdirectory under the home directory.
To switch to any other directory, change the current working directory. This can be done using the cd command – simply type “cd” followed by the directory you’d like to switch to.
To move down in the current directory structure (i.e, to switch to a subdirectory under current working directory), use a period(.) instead of current working directory’s complete path. This saves a bit of precious time. For example: if the current working directory is /home/himanshu and you want to switch to /home/himanshu/Desktop/images you don’t need to type the complete path /home/himanshu/Desktop/images. Simply provide the relative path ./Desktop/images as an argument to cd command.
To move up in the current directory structure, one way is to use the cd command with complete path to the new working directory.
The faster way is to move backwards using ..
Just the way single dot(.) represents current directory, double dot (..) represents previous directory. So, cd ../.. will switch you two directories back.
TIP: If you have to hop repeatedly between two directories – for example, between /home/himanshu and /home/himanshu/Desktop/images – don’t use the cd command followed by complete path – it’s a waste of time. Instead, you can simply use cd -, which functions almost like the “Back” button from your browser:
TIP: Use the [TAB] key to auto-complete directory names. This is very helpful while writing lengthy directory names. For example, instead of writing /home/himanshu, you can just write /home/h. and then press the [TAB] key to request the shell to auto-complete the file name. Note that sometimes there will be multiple folders starting with “h”, and in those cases you’ll be shown a list of such folders.
Provide more and try TAB again.
Learn To Work With Files & Directories
Once CLI navigation is clear, the next important thing is to learn basic file/directory operations.
List Directory Contents
To list every file in a directory use the ls command. For example:
The different colours (see output above) represent different types of files in Linux. Some of the basic colours that you should know are as follows:
To list contents of a directory other than current working directory, input the complete path to that directory as argument to the ls command. For example – ls /home/himanshu/Desktop
Apart from colours (explained above), the file type can also be identified through the file command. For example, in the snapshot (shown below), the file command clearly tells that output1 is a text file.
Display Contents Of A File
To view contents of a file on the command line, use the cat command.
Sometimes it is not possible to view complete file within command prompt shell. This happens when the number of lines in a file is far more than what shell can display. For such huge files, use cat [filename]
The symbol | is known as pipe and is used to direct the output of one command (as input) to another command. Here, it directs output of the cat .bashrc command (as input) to the less command – which makes it possible for a user to view large files smoothly.
Press Enter to scroll the content upwards and q to quit.
Display Size Of A File
To find the size of a file, use -l option with the ls command.
NOTE – Almost every Linux command supports some command line options. These options can be used to produce output in accordance with them. For example, the ls command, when executed with -l option, produces a more comprehensive output compared to when it is executed without any option.
The fifth field in the output (328 in this example) represents size of the file in bytes.
Create A New File Or Directory
To create a new file, use the touch command.
The touch command updates the timestamps (Access, Modify and Change) of a file if it already exists. Access time-stamp represents the date/time when the file was last accessed, modify time-stamp represents the date/time when the file was last modified and change time-stamp represents the date/time when the file’s meta-data was last modified.
The stat command can be used to check the timestamps of a file.
To create a new directory, use the mkdir command.
NOTE – Always input complete path (as argument to the mkdir command) while creating new directory at a location other than current working directory.
Cut, Copy & Rename Files
To copy a file, use the cp command.
cp [source] [destination]
Here is an example that copies a file output from current directory to Desktop :
Home directory path can be replaced with ~ on command line. So, the previous cp command can also be written as :
As /home/himanshu is home directory of a user, so it was replaced with ~.
TIP – Use cd ~ or just cd to switch back to your home directory from anywhere on the command line.
If you’re curious, here is why ~ was chosen to represent home directory.
To move a file from one directory to another (Windows equivalent of cut and paste), use the mv command. Its syntax is similar to that of the cp command.
mv [source] [destination]
The mv command can also be used to rename files.
mv [existing-file-name] [new-file-name]
Search A File Or Text Within A File
To search a file in a directory (and its subdirectories), use the find command.
The find command – shown in the snapshot (see above) – searches the directory /home/himanshu for all the files having .bin extension. Note that * is a wild-card character.
To search text within a file use the grep command.
The grep command searches the file frnd.cpp for lines containing the string #include and displays the result in output. Note that the keyword is displayed in red.
Option -n can be used with the grep command to display line numbers in the output.
To search a string within all the files present in the current directory use asterisk (*) as file name.
* represents everything and so the grep command — shown in the snapshot (see above) — searches for the string #include in all the files present in current directory.
NOTE – Use -R option along with the grep command to search within subdirectories.
Delete Files Or Directories
To delete a file or a directory use the rm command.
Here is an example :
If a file name begins with – (for example -newfile), use — with the rm command to delete it.
To delete a directory use rm -r [directory-name].
You do not have to download a help guide or buy a book to study more about commands in Linux. Manuals for all the standard commands come pre-installed with Linux. Just execute the command man [command-name]
To search a keyword inside a man page, type /[keyword]
For example, to search a keyword file, type /file
and press Enter
Use n to search forward, Shift+n to search backward and q to quit.
The commands discussed in this article are capable of doing a lot more. Practice these examples and go through the man page of each command to know more about it. When you will be done with enough practice on these commands, try to answer the following two questions in comments.
Q1. The mkdir command fails to create the directory structure /home/himanshu/makeuseof/article1. Why?
Here are the contents of /home/himanshu directory for your reference:
Q2. A file named newfile is present in current working directory, but why can the rm not delete it?
Image Credits: rsync + bash Via Flickr