10.1 Linux Commands I Can't Live Without

Saturday, November 8, 2008

10.1 Linux Commands I Can't Live Without

Linux ShellA minimal list of commands a linux / unix user must know. Some of the commands / options may be specific to the shell. Most of these commands, you already use a lot.

Before going to the commands, a short explanation on how to get help on a command...

Level 1: [command] --help
A very minimal help. Just gives the usage and options to use.

Level 2: man [command]
Manual page for the command. Will have explanations on each options and various usages.

Level 3: info [command]
Very extensive information on all aspects of the command.

BTW, all commands may not have man or info.

Let's start...

Example Directory Tree : (you can use the command tree to generate a directory tree)


/home
+--- one
| +--- file1.txt
| +--- file2.txt
+--- two
| +--- file3.txt
| +--- file4.c
| +--- three
| | +--- file5.c
| | +--- file6.h

Locators

/ - The root of Unix Filesystem
~ (tilde) - Home of the current user
. - Present Working Directory (same as using $pwd)
.. - One level up the directory tree

Examples:

If pwd is home/one, . will be home/one and .. will be home.
You can mix . and .. , if you are in home/two/three, ../.. will be home.

You can use these locators with any linux commands.

1. ls

Purpose:
List the contents.

Usage:
ls [OPTION]... [FILE]...

Options:
-l : long display
-a : show all files

I have put alias ll="ls –al" in my .bash_profile.

You can also use ls [path], which will list the contents of [path].

2. vi / vim

Purpose:
A versatile editor.

Usage:
vim [arguments] [file ..] : edit specified file(s)
vim [arguments] - : read text from stdin (the - is needed)
vim [arguments] -t tag : edit file where tag is defined
vim [arguments] -q [errorfile] : edit file with first error

vi - older version
OR
vim - newer version

I have put alias vi="vim" in my .bash_profile.

It is a text editor, but not the normal type, but a special one. A little difficult to learn, but very powerful.

It was a little difficult at first, thought of going back to the old editor. But once I got a hang of it, now in Windows also I normally use VIM.

You can get GVim for Windows http://www.vim.org/download.php#pc.

In Windows, it is quick, easy to use (search etc.), will open any kind of file.

» Also read the Effective Use of VIM series.

3. cd

Purpose:
Change to a specific location.

Usage:
cd [-L|-P] [dir]

Options:
cd - will take you to the previous location.
cd will take you to your home.

4. mkdir

Purpose:
Create a directory.

Usage:
mkdir [OPTION] DIRECTORY...

Options:
-p : Create whole directory tree. (As already explained in Linux Commands I Hardly Knew)

5. cp

Purpose:
Copy files / directories.

Usage:
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

Options:
-r : Recursively copy all files / subdirectories.

You can give a new name for the copied file in the new location.
Eg: cp /home/one/file1.txt /home/two/file5.txt
- This will copy file1.txt and rename to file5.txt.

If you have a lot of files you need to overwrite, then cp will ask for confirmation each time. If your answer is Yes-To-All, then you can use yes | cp -r [SOURCE] [DESTINATION].

6. mv

Purpose:
Rename or Move files / directories.

Usage:
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE...

Options:
-r : Recursively move all files / subdirectories.

7. rm

Purpose:
Remove (unlink) the FILE(s).

Usage:
rm [OPTION]... FILE...

Options:
-r : Recursively move all files / subdirectories.
-f : Force remove / don't ask for confirmation.

8. cat

Purpose:
Concatenate FILE(s), or standard input, to standard output.

Usage:
cat [OPTION] [FILE]...

Normally we use cat to view text files. But the main purpose is concatenation of multiple files.

9. grep

Purpose:
Search for PATTERN in each FILE or standard input.

Usage:
grep [OPTION]... PATTERN [FILE] ...

Options:
-r : Recursive search.
-n : Display line number in files during preview.

Example:
grep -i 'hello world' menu.h main.c

Grep can have Regular Expressions as PATTERN.

Can be used to see just a particular line in a long log file. Just grep for particular message you are looking for, it will show a preview of that line.

10. find

Purpose:
Search for a particular file.

Usage:
find [path...] [expression]

Options:
-iname : Filetype

The combination of find and grep is very powerful, you can find for a particular filename and grep to get files in specific paths only.

10.1. Some extras...
(In Audio, the .1 in 5.1 stands for LFE - Low Frequency Effect)

> : Redirect

The output of one command can be send to a file using >. >> will append to the file.

| : Pipe

The output of one command can be given as input to another command.
Example: find | grep "Long Regular Expression"

xargs

Used to give input data as argument to another command.
Example:
find "longfile.tmp" | xargs rm (Will delete all longfile.tmp files. Check the usage of pipe also)

sed

Very powerful command. Functions include find and replace across file, search etc. VIM is based on sed.

If you are a C programmer, you may use // type comments and /* */ type comments. In pure ANSI C, only /* */ is supported. If you have to migrate to ANSI C, what you do? sed comes to the rescue. Here is the sed command which will convert all // comments to /* */...
sed -ie 's/\/\/\(.*\)/\/\*\1\ \*\//g' [filename]
The credit goes to Balagopal ;)

Backup will be created with a 'e' added to the end of the filename, a.c -> a.ce.

 See also... » Effective Use of VIM

» Linux Commands I Hardly Knew

» Mind Quiz


ATOzTOA : Latest Headlines


6 comments:

dailygyan.com said...

let me add one - exit (or ctrl + d) to quit a terminal.

trench said...

Also...

^z [ctrl + z] = stops/pauses [interrupts] current job. E.g., you run 'firefox' from terminal and you need to jump back to command prompt. Note, this makes said program inoperable for the moment.

jobs = lists current jobs

firefox[1]
something[2]
something_else[3]

kill %1 = In the jobs listed above, it kills firefox. The number following % is the job killed.

fg 1 = brings the job number back to the foreground, removing command prompt once again. If there is only one job running in the bg all you need to type is 'fg'.

^c [ctrl + c] = If you are without command prompt due to a foreground-running job, this kills said job and returns you to prompt.

[job] + & = An example of this would be:

pidgin &

This creates the job but forces it in the background.

An example of the above could be found in a session such as this:

------

user@host$ pidgin
--pidgin opens, cursor blinks, you have command prompt but nothing you type will matter because pidgin is running in the foreground. You can effectively "pause" or "stop" the job with ^z, which renders the job [pidgin] inoperable and returns the following--
^Z
[1]+ Stopped(SIGTSTP) pidgin
user@host fg
pidgin
--pidgin reopens, cursor blinks, you have command prompt but nothing you type will matter because pidgin is running in the foreground again. You can tell the program to gracefully die with ^c, which kills [interrupts] the job [pidgin] and returns the following--
^C user@host$
--so then a more usable job session goes something like this--
user@host$ pidgin &
[1]
user@host$ gimp &
[2]
user@host$ firefox &
[3]
user@host$ jobs
[1] Running pidgin &
[2]- Running gimp &
[3]+ Running firefox &
user@host$ kill %2
gimp: terminated: Terminated
/usr/lib/gimp/2.0/plug-ins/script-fu terminated: Terminated
user@host$ jobs
[1] Running pidgin &
[2]- Done(1) gimp
[3]+ Running firefox &
user@host$ jobs
[1] Running pidgin &
[3]+ Running firefox &

------

The following is a good summary of job control commands: http://tldp.org/LDP/abs/html/x8974.html

That link will also show you the value of having the + and - which appear next to the job numbers in the 'jobs' output.

Anonymous said...

apropos man; if you want to find a command for something, use "apropos [something]" to search the man-pages for whatever command you are searching for.
For example: "apropos compile" returns:
...
c++ (1) - GNU project C and C++ compiler
...
gcc (1) - GNU project C and C++ compiler
...

Post a Comment