Monday, 18 November 2013

Summary of Basic Commands

  • apropos apropos(1) locate commands by keyword lookup
  • arch arch(1B) display the architecture of the current host
  • cal cal(1) display a calendar
    cal [month] year
    • month   number between 1 and 12
    • year      number between 1 and 9999
Examples:
cal 1996      print calendar for year 1996
cal 1 1997   print calendar for January 1997
  • cancel lp(1) send/cancel requests to an LP print service
  • cat cat(1) concatenate and display files (To view files, create files, append to files and combine files)
    cat [options] [files]
    Examples:
    cat files                read file(s)
    cat > file              create file (reads form terminal; terminate input with ^D)
    cat >> file            append to file (reads form terminal; terminate input with ^D)
    cat file2 >> file1   appends contents of file2 to file1
  • cd cd(1) shell built-in functions to change the current working directory
  • chdir cd(1) shell built-in functions to change the current working directory
  • chgrp chgrp(1) change the group ownership of a file
  • chmod chmod(1) change the permissions mode of a file
  • chown chown(1) change owner of file
  • clear clear(1) clear the terminal screen
  • cp cp(1) copy files
  • date date(1) print and set the date
  • dc dc(1) arbitrary precision desktop calculator
  • dos2unix dos2unix(1) convert text file from DOS format to ISO format
  • eject eject(1) eject media such as CD-ROM and floppy from drive
  • exit exit(1) shell built-in functions to enable the execution of the shell to advance beyond its sequence of steps
  • file file(1) file(1B) determine the type of a file by examining its contents
  • head head(1) display first few lines of files
  • lp lp(1) send/cancel requests to an LP print service
  • lpstat lpstat(1) print information about the status of the LP print service
  • ls ls(1) ls(1B) list the contents of a directory
    ls [options] [directories]
    the current working directory used if no directories specified
    A few options:
    • -a    list all entries includeing hidden files (starting with .)
    • -i    print inode numbers
    • -l    long list (mode, links, owner, group, size, timeof last modification, and name
    • -t    sort by modification time
    • -x    multi-column list, sorted across each row
  • Mail, mailx mailx(1), mail mail(1) rmail interactive message processing system to read mail or send mail to users
    mail [options] users
    Examples:
    mail                           with no options, to read your mail
    mail user                     to send mail to user
    mail user < filename     mail a file to another user
  • mkdir mkdir(1) make directories
  • more more(1) browse or page through a text file
  • mv mv(1) move files
  • nispasswd nispasswd(1) change NIS+ password information
  • page more(1) browse or page through a text file
  • pg pg(1) files perusal filter for CRTs
  • pr pr(1) print files
  • ps ps(1) ps(1B) display the status of current processes
  • pwd pwd(1) working directory name
  • rm rm(1) remove files or directories
  • rmdir rm(1) remove files or directories
  • spell spell(1) find spelling errors
  • tail tail(1) deliver the last part of a file
  • umask umask(1) shell built-in function to restrict read/write/execute permissions
  • unix2dos unix2dos(1) convert text file from ISO format to DOS format
  • vi vi(1) screen-oriented (visual) display editor based on ex
  • view vi(1) screen-oriented (visual) display editor based on ex
  • w w(1) who is logged in, and what are they doing
  • wc wc(1) display a count of lines, words and characters in a file
  • which which(1) locate a command; display its pathname or alias
  • who who(1) who is on the system
  • whoami whoami(1B) display the effective current username
  • whois whois(1) Internet user name directory service
  • write write(1) write to another user

Thursday, 14 November 2013

who,w, finger, last - Who are the users?

who command:
It displays us how many number of users are logged in into the server.
who –H
Shows with header.
who –Hu
Shows with header in more details including idle time.
who am i
The total details of current user -you.
whoami
Only the username – your username.
who –b
Time of the last system boot.
who –i
Idle time as Hours:Minutes.
who –r
Print current run level.
who –u
List users logged in including idle time.

w command:
Shows who are logged in and what they are doing.

finger command
it will show two more extra information like office address and phone number.
finger –l
it gives all the information about which shell you are logged in.

last command
it will display how many users are previously logged in and logged off.

uname - what OS it is?

It will display the operating system name.
Uname –r
Shows the version of OS.
Uname –n
Shows the host name of OS.
Uname –m
Shows the micro-processor name.
Uname –v
Shows kernel version.
Uname –p
Shows processor type.

mv - Rename (Move)

This command is used to move the file to some other location. This is also used to rename a file in the same location.
Syntax:
mv <source> <destination>
Ex:
mv F.txt ./A/B/
This will move the file F.txt to directory B inside directory A inside current directory.
mv *.txt ./A/B/
move all the text file to the same location as above.
mv ./A/B/*.txt ./
move back to the initial place from where it was moved as above.
mv –i <source> <destination>
it will ask before move the file to destination.
mv –f <source> <destination>
it will move without prompting even if it is overrating the existing destination.

Qs: what is the difference between cp and mv command?
By cp command source and destination both exist while source is vanished by mv command. After cp command i-node number of source and destination will be different while it remains same in case of mv. Modification time for destination file changes in cp but in mv it remains same.

Monday, 11 November 2013

cp : Copying Files

Cp Command:
The cp command lets you copy a file from one location to another on a UNIX machine.
The cp command overwrites an existing destination file if it is with file contents.
Another use of cp is to create a link of a file.

Syntex:
cp [option] source destination
Source – may be source file name, file with path or directory name, directory with path.
Destination – may be destination file name, file with path or destination directory name, and directory with path.

 

Copy a file or directory from source to destination


cp A.txt B.txt
File A.txt is a source file and B.txt is a destination file, both are inside current directory. So it is not required to mention a path if both source and destination exist inside the same directory.
cp  /home/my_doc/a/f.txt  /home/back_up/
(Absolute Path)
Here the source and destination path is mentioned with filename as both exist in different locations. Absolute path is a path which starts from root.
cp  ./F.txt  ./../../back_up/
(Relative path)
Relative path is a path which starts from current directory.
Here .(dot) means current directory and ..(Double dot) means we are moving one step back/up in directory structure as shown in Fig.

Some examples of cp command:
cp *.txt  ./A/B/
here We have to copy all text file (file with extension .txt) inside current directory to the destination directory B which is inside A and A is inside current directory.

QS: Why sometimes it is not possible to copy a file?
When the file to be copied is read protected or the destination directory is write protected, and then it is not possible to copy a file.
QS: How to copy a whole directory structure?
cp –R <path_of_dir/*> <path_of_destination>
Ex: cp –R ./my_dir/* ./dir_bkp/
Qs: How to copy a file without changing its modification time?
cp -p <source> <destination>
Qs: How to create a link using CP command?
cp –l
cp -s
Qs: how to copy only when the source file is newer than the destination and destination file is missing?
cp -u

cat : Basic File Operation

cat Command:

cat command is basically used for file oriented operations like:
·         Open a file
·         Create a file
·         Write to a file
·         Modify a file
·         Append to a file
·         Combine files
There are also some other operations where cat command involves:
·         Creating Zero byte(NULL) file
·         Counting lines in a file
·         Displaying Non printing ASCII characters

There are some redirection operators which is used with cat (as well as other command too), these are:
<       input redirection operators
>       output redirection operators
>>     append redirection operators
<<     here document
>$      associate operator   



Opening a file in READ ONLY manner:
cat <file_name>
e.g. cat F.txt
if F.txt is a file name.
Opening multiple files in READ ONLY manner:
cat A.txt B.txt C.txt
Here 3 files will be open in a sequence as the names are given in command, this way you can use this command to open many number files as you wish.

Creating a new file:
cat > F.txt
This is a test.
Unix is the best.
[ctrl+d]


Writing into a file which is created before / modifying the contents into a file:

cat > F.txt
This is a test.
Unix is the best.
[ctrl+d]


Append to a file:

cat >> F.txt
This is a test.
Unix is the best.
You should learn UNIX

UNIX and linux has same flavor.

[ctrl+d]


Combining two/multiple files:

Appending contents of one file to another file:

Cat F.txt >> G.txt

After this command when you open file G.txt you will see contents of F.txt after the contents of G.txt.  

Redirecting/ Overrating contents of one file to another file:

cat F.txt > G.txt

After this command when you open file G.txt you will see only the contents of F.txt is remaining and contents of file G.txt is vanished i.e. contents of file F.txt is overwritten into G.txt.

Redirecting contents of multiple file into one file/ Combining multiple files:

cat A.txt B.txt C.txt > F.txt

Appending contents of multiple file into one file/ Combining multiple files:

cat A.txt B.txt C.txt >> F.txt





cat with options/ other uses of cat command:

Creating Zero byte(NULL) file:
cat F.txt > F.txt
Keep the same file both side of output redirection operator.

Counting lines in a file
cat –n F.txt
This will count total number of line including the blank lines in a file F.txt. Line numbers will be displayed at the beginning of every line.
cat –b F.txt
This will count total number of line excluding the blank lines in a file F.txt. Line numbers will be displayed at the beginning of every line.

Displaying Non printing ASCII characters
cat –v F.txt
Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly. ASCII control characters (octal 000 - 037) are printed as ^n, where n is the corresponding ASCII character in the range octal 100 - 137 (@, A, B, C, . . ., X, Y, Z, [, \, ], ^, and _); the DEL character (octal 0177) is printed ^?. Other non-printable characters are printed as M-x, where x is the ASCII character specified by the low order seven bits.

Display $ at the end of every line
cat –e F.txt
You can make the cat to display the $ character at end of every line. Normally by listing file contents, users cant identify whitespaces at the end of each lines, by using the cat -e option.

 

Find out TAB characters

cat –T F.txt
Use option -t to display the tab characters. It displays ^I for TAB character.


Use Cat Command with More & Less Options

cat F.txt | more
cat F.txt | less
If file having huge contents that will not fit in output terminal and screen scrolls up very fast and you are not able to see whole content at once, we can use parameters more and less with cat command as show above. This will show you file contents page by page while pressing spacebar.