-->

Basic Linux Commands

Posted by Admin on
Pwd:
pwd command will print your home directory on screen, pwd means print working directory.

/u0/ssb/ishant
is output for the command when I use pwd in /u0/ssb/ishant directory.

Ls:
ls command is most widely used command and it displays the contents of directory.

options :
ls will list all the files in your home directory, this command has many options.
ls -l will list all the file names, permissions, group, etc in long format.
ls -a will list all the files including hidden files that start with . .
ls -lt will list all files names based on the time of creation, newer files bring first.
ls -Fx will list files and directory names will be followed by slash.
ls -R will lists all the files and files in the all the directories, recursively.
ls -R | more will list all the files and files in all the directories, one page at a time.


Mkdir:
mkdir sandeep will create new directory, i.e. here sandeep directory is created.

Cd:
cd ishant will change directory from current directory to ishant directory.

Use pwd to check your current directory and ls to see if ishant directory is there or not.
You can then use cd ishant to change the directory to this new directory.

Cat:
cat cal.txt cat command displays the contents of a file here cal.txt on screen (or standard out). 

Head:
head filename by default will display the first 10 lines of a file. 
If you want first 50 lines you can use head -50 filename or for 37 lines head -37 filename and so forth. 

Tail:
tail filename by default will display the last 10 lines of a file. 
If you want last 50 lines then you can use tail -50 filename. 

More:
more command will display a page at a time and then wait for input which is spacebar. For example if you have a file which is 500 lines and you want to read it all. So you can use 
more filename 

Wc: 
wc command counts the characters, words or lines in a file depending upon the option. 

Options
wc -l filename will print total number of lines in a file. 
wc -w filename will print total number of words in a file. 
wc -c filename will print total number of characters in a file. 

File:
File command displays about the contents of a given file, whether it is a text (Ascii) or binary file. To use it type

file filename. For example I have cal.txt which has ascii characters about calander of current month and I have resume1.doc file which is a binariy file in microsoft word. I will get
file resume.doc
resume1.doc:    data
file cal.txt
cal.txt:        ascii text

Cp:
cp command copies a file. If I want to copy a file named oldfile in a current directory to a file named newfile in a current directory.
cp oldfile newfile
If I want to copy oldfile to other directory for example /tmp then
cp oldfile /tmp/newfile. Useful options available with cp are -p and -r . -p options preserves the modification time and permissions, -r recursively copy a directory and its files, duplicating the tree structure.

Mv:
mv command is used to move a file from one directory to another directory or to rename a file. 

Some examples:
mv oldfile newfile will rename oldfile to newfile. 
mv -i oldfile newfile for confirmation prompt. 
mv -f oldfile newfile will force the rename even if target file exists. 
mv * /usr/bajwa/ will move all the files in current directory to /usr/bajwa directory. 

In:
Instead of copying you can also make links to existing files using In command. 
If you want to create a link to a file called coolfile in /usr/local/bin directory then you can enter this command. 
ln mycoolfile /usr/local/bin/coolfile
Some examples:
ln -s fileone filetwo will create a symbolic link and can exist across machines. 
ln -n option will not overwrite existing files. 
ln -f will force the link to occur. 

Rm:
To delete files use rm command.

Options:
rm oldfile will delete file named oldfile.
rm -f option will remove write-protected files without prompting.
rm -r option will delete the entire directory as well as all the subdirectories, very dangerous command.

Rmdir:
rmdir command will remove directory or directories if a directory is empty. 

Options:
rm -r directory_name will remove all files even if directory is not empty. 
rmdir sandeep is how you use it to remove sandeep directory. 
rmdir -p will remove directories and any parent directories that are empty. 
rmdir -s will suppress standard error messages caused by -p


Diff:. 
diff command will compare the two files and print out the differences between. 
Here I have two ascii text files. fileone and file two. 
Contents of fileone are 
This is first file 
this is second line
this is third line
this is different    as;lkdjf 
this is not different

filetwo contains 
This is first file
this is second line
this is third line

this is different    xxxxxxxas;lkdjf
this is not different

diff fileone filetwo will give following output 
 4c4
< this is different    as;lkdjf
---
> this is different    xxxxxxxas;lkdjf

Cmp:
cmp command compares the two files. For exmaple I have two different files fileone and filetwo. 
cmp fileone filetwo will give me 
fileone filetwo differ: char 80, line 4 

if I run cmp command on similar files nothing is returned.
-s command can be used to return exit codes. i.e. return 0 if files are identical, 1 if files are different, 2 if files are inaccessible. 
This following command prints a message 'no changes' if files are same 
cmp -s fileone file1 && echo 'no changes' . 
no changes

Dircmp:
dircmp command compares two directories. If i have two directories in my home directory named
dirone and dirtwo and each has 5-10 files in it. Then 
dircmp dirone dirtwo will return this 
 Dec  9 16:06 1997  dirone only and dirtwo only Page 1

./cal.txt                                   ./fourth.txt
./dohazaar.txt                              ./rmt.txt
./four.txt                                  ./te.txt
./junk.txt                                  ./third.txt
./test.txt 


Find:
Find command is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each pathname and finds the files that meet the specified conditions. Here are some examples.

Some Examples:
find $HOME -print will lists all files in your home directory. 
find /work -name chapter1 -print will list all files named chapter1 in /work directory. 
find / -type d -name 'man*' -print will list all manpage directories. 
find / -size 0 -ok rm {} \; will remove all empty files on system. 

Conditions of find 

-atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days. 
-ctime +n or -n will find that were changed +n -n or n days ago. 
-depth descend the directory structure, working on actual files first and then directories. You can use it with cpio command. 
-exec commad {} \; run the Unix command on each file matched by find. Very useful condition. 
-print print or list to standard output (screen). 
-name pattern find the pattern. 
-perm nnnfind files whole permission flags match octal number nnn. 
-size n find files that contain n blocks. 
-type c Find file whole type is c. C could be b or block, c Character special file, d directory, p fifo or named pipe, l symbolic link, or f plain file. 

User account commands:

Passwd: Change your password. 
This will let you enter a new password. Please use a password that is not a real word or name and has numbers or punctuation in it. 
# passwd 

Hello : Become another user 
“ Hello username” attempt to login as another user, system authenticate by prompting password. Without specifying username, it is attempt to login as root. 

exit : Logout 
When you are login as a user, you logout by this command. 

whoami : Check the current being user.



Please comment if you like the above post or if you find any mistake .



No comments:

Post a Comment