Академический Документы
Профессиональный Документы
Культура Документы
get-started-with-the-linux-command-line%2F&client=ca-pub-9894449314507324)
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.
If you are absolutely new to Linux, try reading our Linux guide (http://www.makeuseof.com/pages/getting-started-guide-linux). Also, learn
about how you can replace Windows with Linux (http://www.makeuseof.com/tag/making-linux-a-genuine-windows-replacement/) and how
to run Windows applications on Linux (http://www.makeuseof.com/tag/how-about-some-wine-with-your-linux/).
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 1/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
To see the complete path of your working directory, always use the pwd command.
get-started-with-the-linux-command-line%2F&client=ca-pub-9894449314507324)
$79 Ls Ls
carhidkits.com
Wholesale. German Quality Ships from Minneapolis, MN
Repair Windows Update
Sound File Converter
Enter PCH Sweepstakes Now
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 :
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 2/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 3/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 4/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
Once CLI navigation is clear, the next important thing is to learn basic file/directory operations.
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
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 5/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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.
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] | less .
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 6/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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.
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 7/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
The fifth field in the output (328 in this example) represents size of the file in bytes.
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 8/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
NOTE – Always input complete path (as argument to the mkdir command) while creating new directory at a location other than current
working directory.
cp [source] [destination]
Here is an example that copies a file output from current directory to Desktop :
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 9/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
Home directory path can be replaced with ~ on command line. So, the previous cp command can also be written as :
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 (http://unix.stackexchange.com/questions/34196/design-question-
why-was-chosen-to-represent-the-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]
mv [existing-file-name] [new-file-name]
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 10/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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 (http://www.tldp.org/LDP/GNU-Linux-Tools-Summary/html/x11655.htm).
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.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 11/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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.
rm [file-name]
Here is an example :
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 12/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
If a file name begins with – (for example -newfile), use — with the rm command to delete it.
Man Pages
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 13/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
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] and a manual page for that command will open. For
example, here’s man rm.
To search a keyword inside a man page, type /[keyword] and press enter.
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 14/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
Conclusion
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?
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 15/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
Q2 . A file named newfile is present in current working directory, but why can the rm not delete it?
Mail (mailto:?body=http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-
line/&subject=A Quick Guide To Get Started With The Linux Command Line)
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 16/17
12/13/2015 A Quick Guide To Get Started With The Linux Command Line
Master the Linux Com… Join Linux for New Switchers Join Best Linux Apps Join
134 Members 106 Members 94 Members
(http://grou.vi/gic) (http://grou.vi/eic) (http://grou.vi/aic)
http://www.makeuseof.com/tag/a-quick-guide-to-get-started-with-the-linux-command-line/ 17/17