adsense

Thursday, March 15, 2012

Computer Engineering: Most Common Linux Commands and Shortcuts


Like many newcomers to Linux, I was afraid of the command line when I first started using Linux. However, I began to realize that without the command line I wasn't exploiting my computer to the maximum. That's why it is helpful to know the most common Linux commands and shortcuts you can safely use.

Introduction

Manual Page of ls Command
You are not really exploiting your Linux box to the maximum if you are not using the command line. The tasks that could take hours to perform in the graphical user interface are just a couple of seconds away if you are using the terminal. For example assume that your friend requested to know which songs you have in your music collection. Would you prefer opening up a file browser and checking each song or search string, or would you prefer to open up a terminal and type tree Music > my_music.txt, which also shows which songs are in which folder and writes them to a text file ready to e-mail? I assume you chose the latter, as it is quicker and provides a copy of said search parameters, and if so, then it pays to learn the basic Linux commands and to start using them step by step, day after day.
Commands in the Linux world are issued as command options arguments. To learn more about commands, options and arguments always consult the man (manual) page first. To read the man pages, type man command_name. The screenshot above is an example of a man page, for the ls command.

I will give links throughout the article for each command, which will take you to the article which is written on this specific one.

cp

Possibly the oldest command that lets you copy one file from one location to the other. The usage is cp folder_name/file1 /another_folder/file2 which copies the file named file1 in folder_name to file2 in another_folder.
Also note, this command string renames the file during the copying process, for example here we want to give file1 another name, i.e. file2.
To learn more about the cp command check out Jon Jermey's article Moving and Copying Linux Files.

mv

Possibly the second oldest command which allows you to move files from one location to the other. The usage is the same with cp.

ls

Another archaic command, and short for list. Yes, you are right! the ls command simply lists the contents of the particular directory you are in. For example if you are in your home folder, ls lists the home directories contents and its underlying files. However, ls does not list hidden files if there are no accompanying arguments stated. To see those files, use the -a option: ls -a.

cd

The cd command is short for change directory. It allows you to change from one directory to another. For example, if you are in your /home/user_name/Documents folder and you want to change to the /home/user_name/Music folder, you can issue cd ~/Music and change to that directory. The tilde (~) character is the shortcut for your home folder.

df

The df command displays how much space you are using on your disk. Note that it displays this information in 1 Kilobyte blocks if you do not stipulate a preference. Therefore I recommend you to go with the -H option, which tells the df command to output the results in human readable format (in Gigabytes or Megabytes.)

cat

The cat command displays the contents of a file, which is very useful when looking at text files and there makeup. The usage is cat filename but it is more commonly used in conjunction with other commands.

more & less

more and less commands are used to display the screen one page at a time and are used with other commands. For example, if you want to see the contents of a text file which normally take 4 pages, issuing acat filename command scrolls the results so fast that you will not be able to see the first three pages. To see one page at a time, you have to issue cat filename | more or cat filename | less.
Both commands are the same in terms of basic functionality. You can check their man pages and use the one you prefer.

top

The top command displays the top ten processes that are using computer resources at that particular time.

grep

The grep command is the abbreviation of global regular expression print, which searches an input file for text and displays the output. It is mostly used together with other commands. An example would be trying to find Firefox in your currently running processes, and to do this you would type the command ps -aux | grep firefox.

tree

As is the example I have given in the introduction section, the tree command lists the subfolders and file structures inside of them, in a “tree” fashion.

Conclusion

All of the above are “harmless” commands if you are using them as a regular user in your Linux system. Open up a terminal and try to take these baby steps and you will soon become familiar with syntax, data structure and movement of files. The worst thing that you can do with these commands is move a folder from a known place to somewhere else! Not that scary, as you can easily do the reverse and move it back to its rightful place.
One thing that I must stress however, the Linux command line does not ask you any confirmation questions like other more common operating systems do, i.e. “are you sure you want to copy this file from this location?” It assumes that you already know what you are doing.

It simply accepts the command (provided the syntax is correct) and does as requested. In extreme cases this can cause major issues with system stability, for example issuing the command rm -rf, tells Linux to remove all folders recursively and by force, which will end up emptying your folder. It does not ask if you are sure about such actions; it simply executes them. Just imagine what could happen if you issued this command as root.

No comments:

Post a Comment