Linux Tutorials for commands :set-1(Starter)

This linux command tutorial contains ten basic commands for starting purpose.

 1.Pwd :”print working directory”

The pwd command shows you your current position within the Linux filesystem. The position is known as your ‘current working directory’.

Ex:suppose i am working in “/dir1/dir2″ directory

then

$pwd

/dir1/dir2

It shows me current working directory path.

2.ls :”Listing files”


The ls command lets you list files. For example, here is the (shortened) output of an ls command on my system:

$ ls
 artdir    countdir   machine
 tsardir   sortdir
$

Alternatively, you can get a ‘long listing’ that shows file sizes, timestamp, ownership, and permissions as follows:

$ ls -l
 drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 artdir
 drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 countdir
 drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 machine
 drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 sortdir
 drwxr-xr-x 2 rayy rayy 4096 Feb 3 2002 tsardir
$

You can also supply a target directory to the ls command. For example, to view the contents of the /tmp directory, I enter the following:

$ ls /tmp
 flp     kde-rayy   mcop-rayy
$

For more information on the ls command you can reference the manual page for ls with the following command:

$ man ls

3.cd :”change current directory”
This next command, ‘cd’, lets you change your current working directory. for example, you can change your current working directory to /usr/bin by entering the following command:

 [rayy barton0 rayy]$ cd /usr/bin
 [rayy barton0 bin]$

Note that after I entered the cd command, my command prompt changed to reflect the change in the last node of my current working directory. Your command prompt may not be configured to do that.

Change your current working directory to /usr/bin now and enter the ls command.

 [rayy barton0 code]$ cd /usr/bin
 [rayy barton0 bin]$ ls
 addr2line         mcheck
 addr2name.awk     mcomp
 addresses         mcookie
 .
 .
 .
 [rayy barton0 bin]$

The preceding is a partial listing. There are many, many files in the /usr/bin directory on most Linux systems.

4.file :”to see all code files”
If you have a background in Windows or are familiar with DOS, you are used to file extensions that signify the file type. Linux (and Unix) have no such requirement. That is, an executable program can be named anything. Therefore, a handy command is supplied with Linux named ‘file’. For example, I have a file named ‘sample.c’ in my code directory. I can learn a bit about that file by entering the following command:

 [rayy barton0 code]$ file sample.c
 sample.c: C++ program text
 [rayy barton0 code]$

Alternatively, I can use the ‘*’ wildcard — which represents all filenames — to examine all of my code files at once. The following is a shortened example:

 [rayy barton0 code]$ file *
 bbsdir:     directory
 code.tar:   GNU tar archive
 genart.c:   ASCII C program text
 sample.c:   C++ program text
 xor:        ELF 32-bit LSB executable
 [rayy barton0 code]$

The file command can be very useful to avoid minor annoyances — such as when using one of the following three commands.

5.cat
The cat command is useful for concatenating multiple files — or just for dumping a single text file to the screen. Before you use the cat command to dump a file to the screen, use the file command to make sure it’s some variety of text file such as ascii text, commands/text, C source code, html/text, etc. The following is a shortened example of using file and cat to identify and dump a text file:

 [rayy barton0 code]$ file xor.c
 xor.c: ASCII C program text
 [rayy barton0 code]$ cat xor.c
 main()
 {
     int x;
     unsigned char buff[128],
 .
 .
 .   
 [rayy barton0 code]$

6.more
The more command is useful when a text file is larger than a single screen. The following is a shortened example of using more to view a large C program:

 [rayy barton0 code]$ more xor.c
 main()
 {
     int x;
     unsigned char buff[128],
 .
 .
 .   
     printf("n%sn",buff1);
     printf("n%sn",buff2);
 }
 --More--(29%)

Note the ‘–More–(29%)’ at the end of the screen. That means that 29% of the file is above that line, implying that another 71% of the file is below. Press the space bar to page through the file, a screenful at a time. Press the b key to back up. If you finish looking before reaching the end of the file, press the q key to quit.

7.grep
The grep command, short for ‘get regular _expression_ and print’, is useful for finding occurances of a particular string in a text file. To find the ‘printf’ statements in the example C program above, enter the following command:

 [rayy barton0 code]$ grep printf xor.c
 printf("n%sn",buff1);
 printf("n%sn",buff2);
 [rayy barton0 code]$

The grep command has far more capability than I describe here and, as usual, enter

 [rayy barton0 code]$ man grep

for more information.

8.cp
The cp command will let you copy files. Unlike the commands used above, this one includes a hazard; if you copy filename1 to filename2 and filename2 already exists, you will destroy the original filename2 file. Use cp with caution!

To make a duplicate copy of my xor.c file I could enter the following command:

 [rayy barton0 code]$ cp xor.c xor.c.bak
 [rayy barton0 code]$ ls xor.c*
 xor.c  xor.c.bak
 [rayy barton0 code]$

Note that the cp command returned no output — I had to enter an ls command to see the results of the copy. [By adding the * wildcard to the original filename, I asked for a listing of all files that started with xor.c -- including those with no additional characters in the name.]

9.rm
The rm command is used for removing files. To remove the duplicate file I created in the cp command example, I would enter the following:

 [rayy barton0 code]$ rm xor.c.bak
 [rayy barton0 code]$ ls xor.c*
 xor.c
 [rayy barton0 code]$

Again, note the absense of any feedback from the rm command. I had to enter an ls command to verify that the xor.c.bak file had really been removed.

As with other commands, rm can remove multiple files at once when used with wildcards or with the -r (recursive) option. See the man page for more information on rm.

10.mkdir/rmdir
Ok, this is really two commands, but they are complementary. Use the mkdir command to make a new directory and use the rmdir command to remove an empty directory. For example:

 [rayy barton0 tmp]$ mkdir testdir
 [rayy barton0 tmp]$ ls
 testdir
 [rayy barton0 tmp]$ rmdir testdir
 [rayy barton0 tmp]$ ls
 [rayy barton0 tmp]$

In the preceding series of commands I first created a new directory named ‘testdir’. I then used the ls command to verify its presence. Then, I removed ‘testdir’ and verified that it was gone by using ls again

This entry was posted in Programing, Unix and tagged , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>