Pinterest Stumbleupon Whatsapp
Ads by Google

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. Also, learn about how you can replace Windows with Linux Making Linux a Genuine Windows Replacement Making Linux a Genuine Windows Replacement With Windows 8 casting a long shadow over the PC industry and Valve committing to create Linux-based gaming PCs, there's never been a better time to start using Linux. For many users, the Linux desktop... Read More and how to run Windows applications on Linux Run Windows Applications on Linux (or Mac) with WINE Run Windows Applications on Linux (or Mac) with WINE Read More .

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.

01-image-pwd-command

Ads by Google

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.

01-01-image-cd-command

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.

01-1-image-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. 

01-02-image-cd-command

The faster way is to move backwards using .. 

01-2-image-cd-command

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:

01-image-cd-command

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.

01-image-cd-tab

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:

09-1-image-ls-command

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:

Linux File ColorsTo 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.

03-image-file-command

Display Contents Of A File

To view contents of a file on the command line, use the cat command.

04-image-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 .

04-01-image-cat-less

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.

04-02-image-less-display

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.

04-1-image-ls-l-command
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.

05-1-image-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.

05-1-image-stat-command

To create a new directory, use the mkdir command.

05-2-image-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 :

05-2-image-cp-command
Home directory path can be replaced with ~ on command line. So, the previous cp command can also be written as :

05-3-image-cp-command

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.

06-image-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.

07-1-image-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.

07-2-image-grep-command

To search a string within all the files present in the current directory use asterisk (*) as file name.

08-image-grep-command

* 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.

rm [file-name]

Here is an example :

09-0-image-rm-command

If a file name begins with – (for example -newfile), use — with the rm command to delete it.

09-3-image-rm-command

To delete a directory use rm -r [directory-name].

10-3-image-rm-command

Man Pages

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.

11-image-man-command

To search a keyword inside a man page, type /[keyword] and press enter.

For example, to search a keyword file, type /file

11-1image-man-search

and press Enter

11-2-image-man-search-results

Use n to search forward, Shift+n to search backward and q to quit.

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?

14-2-image-mkdir-command

Here are the contents of /home/himanshu directory for your reference:

14-1-image-ls-command

Q2. A file named newfile is present in current working directory, but why can the rm not delete it?

15-1-image-ls-command

15-2-image-rm-command

Image Credits: rsync + bash Via Flickr

  1. Sultaan
    January 8, 2016 at 11:27 am

    Thanks Himanshu :)
    Q. For different file types, there are different colors assigned. How to view/modilfy those colors of the CLI ?

  2. Art Werschulz
    March 17, 2015 at 1:50 pm

    Nice tutorial. I'm recommending it to my programming students, see
    http://www.dsm.fordham.edu/~agw/unix-linux-links.html

    Some nitpicking:

    (1) Rather than "cat filename|less" to view a large text file, why not simply "less filename"? (In the Usenet unix newgroups, this would be labelled "UUOC"..."useless use of cat".) Of course, this does give you the opportunity to introduce piping; maybe use a different example (such as the ever-popular "who|wc -l" to count the number of users on the system) for introducing same.

    (2) I probably wouldn't mention the stat command for something this introductory.

  3. Pooky J
    September 6, 2013 at 10:58 am

    You say Linux but you use a photo of MacBook, man

    • Himanshu
      September 7, 2013 at 1:11 am

      Thanks for pointing out. Its corrected now.

  4. Like Fun B
    September 5, 2013 at 2:18 pm

    The way to gain command line proficiency is to actually USE the command line, which very few systems are configured to need. On *nix, learning your shell is the first step to learning scripting, but that's not a goal many people have, so I usually teach neophyte techies to ssh in to a system where their reward for patiently working through my exercises is a bunch of cute/funny pictures; on a PC I use a very simple challenge of setting up a DOS environment to play a game. The concepts are largely transferable even if the commands are far from identical.

    You don't mention the concepts associated with text editing, but anyone who is working at a *nix command line who can't use vi is probably a half-step away from being crippled the first time they run across a system without nano.

    • Himanshu
      September 7, 2013 at 1:15 am

      I think text editing through command line editors like vi/vim is not as easy as some of the commands mentioned here and hence requires a dedicated article. I'll come up with one soon. Thanks.

  5. Frank P
    September 5, 2013 at 2:01 pm

    incase you forget where you found this page, here is an app for android devices, that can help you aswell:
    https://play.google.com/store/apps/details?id=com.chmod0.manpages

    • Himanshu
      September 7, 2013 at 1:17 am

      Thanks for Sharing. :-)

  6. Milinda L
    September 5, 2013 at 10:36 am

    Good job Friend!

    • Himanshu
      September 8, 2013 at 2:43 am

      Thanks Milinda.

  7. Chuck P
    September 5, 2013 at 1:49 am

    One note on colors for files. All color is not the same, what might be used in Ubuntu, will not the be same in Fedora, Slackware, or openSUSE.

    • Himanshu
      September 7, 2013 at 1:21 am

      Well, I think the default colors should be same across distributions but I am not quite sure. Do you have a something in support of your argument?

  8. Andreev
    September 4, 2013 at 7:46 pm

    For Q1, we cannot create sub-directory of non excisting directory. So we first have to make mkdir ~/makeuseof and then mkdir ~/makseuseof/article.
    For Q2, the file cannot be deleted, because we have to give the option -r or -rf.

    • myself
      September 4, 2013 at 9:23 pm

      no to your answer to q2: imho you need a esc char for the leading space
      @himanshu great article! pls give us more, perhaps with a tad regex !

    • Markus Klyver
      September 4, 2013 at 10:50 pm

      You can. Just use the flag -p. To follw your example it would be
      mkdir ~/makseuseof/article

    • Markus Klyver
      September 4, 2013 at 10:52 pm

      Opps, forgot the flag.
      mkdir -p ~/makseuseof/article
      is the correct command

  9. Aibek
    September 4, 2013 at 7:00 pm

    learned a few things here) thanks Himanshu!

    • Himanshu
      September 5, 2013 at 12:00 am

      You are welcome Aibek :-)

Leave a Reply

Your email address will not be published. Required fields are marked *