Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unix Commands2

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

UNIX COMMAND DESCRIPTION

Day 1:
1.cat command :

The cat command reads one or more files and prints them to standard output. The
operator > can be used to combine multiple files into one. The operator >> can be used to
append to an existing file.

Syntax: cat [options] [files]

Options:

 b - number non-blank output lines


 n - number all output lines
 s - squeeze multiple adjacent blank lines
 v - displays nonprinting characters as if they were visible, except for tabs and the end of line
character
 A -show all characters, also tabs and end-of-line characters as ^I and $

2.ls command :

The ls command lists all files in the directory that match the name. If name is left blank, it will
list all of the files in the directory.

Syntax: ls [options] [names]

Options:

 a – displays all the files including hidden files


 l – uses a long listing format
 r – shows o/p in reverse order
 R – lists all files in the sub directories recursively
 t – sorts file by modification time
 h- shows file size in human readable format
 u – sorts by access time
 C – list entries by columns
 1 – shows single entry per line
 m – displays file list separated by comma
 S – sorts by file size
 F – classified dir, exec file, spec file by appending /,* and @ respectively
3.ps command :
The ps command displays active processes.

Syntax: ps [options]

Options:

 e – Displays all active processes.


 f - Displays a full listing of processes and commands active.
 a – List of processes being executed by other users.
 p list - Displays data for the list of process IDs.
 t list - Displays data for the list of terminals.
 u list - Displays data for the usernames.

4.wc command :

Shows the no of lines, chars and word in the files.

Syntax: wc [options] [files]

Options:

 l – prints the number of lines


 c – prints the number of chars
 w– prints the number of word
 L – prints length of longest line

5.sort command :

Normally sorts on the basis of ascii value.

Syntax: sort [options] [files]

Options:

 k – Sorts on the Specified key.


 t – Specifies the field delimiter (default is space).
 n – Sorts in numerical order.
 c – Checks whether the file is already sorted or not.
 r – Sorts in reverse order.
 u – Sorts and shows only unique value.
 o – Writes the sorted contents into specified file.
6.cut command :

cut command slices a line and extracts the text.

Syntax: cut [options] [files]

Options:

 f – Specifies a field list, separated by a delimiter


 d– Specifies the field delimiter (default is tab).
 c – Specifies the range of chars.

7.grep command :

The grep command allows you to search one file or multiple files for lines that contain a pattern

Syntax: grep [options] pattern [files]

Options:

 i –case insensitive search.


 c – Returns the no of line matches the pattern.
 n – shows the line no. in the output
 v – Shows the record that doesn’t match the pattern.
 l – Shows the name of file that contains the pattern.
 w – Returns the line where the exact pattern matches.
 r – Searches in all files recursively within a dir and sub dir.
 e – multiple pattern search
 A n - display the matched lines along with the n lines after the matched lines.
 B n - display the matched lines along with the n lines before the matched lines.
 C n - display the matched lines along with the n lines around the matched lines.
Day 2:

8.touch command:

touch used to change a file's access and modification timestamps. It is also used to create a new
empty file

Syntax: touch [options] <file_name>

Options:

 a - change the access time only.


 m - Change the modification time only.
 c -if the file does not exist, do not create it and do not report this condition.(doubt).
 d date_time - use the date_time specified to update the access and modification times.
 t time - use the time specified to update the access and modification times.
 r file - use the access and modification times of file.

9.chmod command:

The chmod command changes the access mode of one file or multiple files.

Syntax: chmod [option] mode files

Options:

 R - Include objects in subdirectories (recursive).


 f – Suppress error messages if command fails.
 v– Show objects processed.

Modes:

Mode descriptions

Who u=user, g=group, o=other, a=all (default)

Opcode + means add permission

- Means remove permission

= means assign permission and remove the permission of unspecified fields


Permission r=Read, w=write, x=Execute, u=user, g=group, o=other.

Practiced examples:

1. chmod 0664 amiya.dat

2. chmod g+rw amiya_dir

3. chmod a-w myfile.dat

4. chmod ug=rx mydir

10.kill command:

kill command is used to send a signal to each process specified by a pid (process identifier). The
default signal is SIGTERM (terminate the process).

Syntax:

kill PID

kill -s signalName PID

kill -singnalNumber PID

Common UNIX Signal Names and Numbers:

Number Name Description Used for


0 SIGNULL Null Check access to pid
1 SIGHUP Hang up Terminate; can be trapped
2 SIGINT Interrupt Terminate; can be trapped
3 SIGQUIT Quit Terminate with core dump; can be
9 SIGKILL Kill Forced termination; cannot be trapped
15 SIGTERM Terminate Terminate; can be trapped
24 SIGSTOP Stop Pause the process; cannot be trapped
25 SIGTSTP Terminal stop Pause the process; can be
26 SIGCONT Continue Run a stopped process
Examples:

1. kill 4242

2. kill -s SIGKILL 1414

3. kill -9 1414

11. head command:

head prints the first N number of data of the given input. By default, it prints first 10 lines of
each given file.

Syntax: head [options] <file_name>

Options:

 c N- To print N bytes from each input file.


 n N- to print N lines from each input file(default, can ignore n option).
 q - Never print headers giving file names.
 v - Always print headers giving file names.

12. tail command:

Tail prints the last N number of lines from given input. By default, it prints last 10 lines of each
given file.

Syntax: tail [options] <file_name>

Options:

 c N- To print N bytes from each input file.


 n N- to print N lines from each input file(default, can ignore n option).
 q - Never print headers giving file names.
 f - To print appended data as and when the file grows.
 s - To sleep for N seconds between iterations.
 v - Always print headers giving file names.
13. more command:
The more command displays the file called name in the screen. The RETURN key displays the
next line of the file. The spacebar displays the next screen of the file.

Syntax: more [options] <file_name>

Options:

 -num - This option specifies an integer which is the screen size (in lines).
 +num - Start at line number num.
 -s - Squeeze multiple blank lines into one.
 -d - more will prompt the user with the message.
 -p - Do not scroll. Instead, clear the whole screen and then display the text.

Day 3:

14. cd command:

The cd command is used to change from the current directory to another directory.

Syntax: cd [directory]

Options with example:

 no attributes Return to the home directory


 cd . . - changes the directory to its parent directory by going back one level. The space
between the cd and .. is must.
 cd ../.. - takes you back to two directories. You can extend this cd command to go back
to n number of directories.
 cd - - go back to the previous directory.
 cd ~ username - put you in username's home directory.
 cd dir – (without a /) will put you in a subdirectory. For example, if you are in /usr,
typing cd bin will put you in /usr/bin, while cd /bin puts you in /bin.

15. cp command:
cp is a UNIX command for copying files and directories.

Syntax: cp [OPTIONS] < sourcefile destfile >

Options:

 -f - if an existing destination file cannot be opened, remove it and try again.


 -r or –R - copy directories recursively.
 -i - prompts you with the name of a file before overwrite.
 -l - link files instead of copying.
 - L - follow symbolic links rather than hard link.
 -p - preserves the following characteristics of each source path in the corresponding
target: modification date, access date, ownership, file permission.

16. ln command:

ln is used to create links or link to files.

There are two types of links, both of which are created by ln:

 Symbolic links - which refer to a symbolic path indicating the location of another file.
 Hard links - which refer to the specific location of physical data.

Syntax: ln [OPTIONS] < filename linkname >

Options with example:

 No option – creates hard link.


 -f - force removal of existing files to allow the link to be created.(forces hard link).
 -s - create symbolic links.
 -fs – forces symbolic link.
 -n - In order to link to a folder.

17. mkdir command:

The mkdir command creates a single directories or multiple directories.

Syntax: mkdir [options] directories

Options:

 -m mode - Sets the access mode for the new directory.


 -p - if the parent directories don't exist, this command creates them.
Example :

mkdir -m 444 tech

mkdir -p tech/net/faqs

Day 4:

18.sudo command :

 sudo allows you to run a UNIX command as a different user.


 This command effectively elevates your access rights, thus allowing you to run
commands and access files which would otherwise be not available to you.
 sudo su – gersfabi: Which will allow you to use access rights of user gersfabi but it will
ask your password instead of gersfabi password.

19.scp command :

scp allows files to be copied to, from, or between different hosts. It uses ssh for data transfer
and provides the same authentication and same level of security as ssh.

Examples:
 Copy the file "foobar.txt" from a remote host to the local host
$ scp your_username@remotehost.edu:foobar.txt /some/local/directory
 Copy the file "foobar.txt" from the local host to a remote host
$ scp foobar.txt your_username@remotehost.edu:/some/remote/directory
 Copy the directory "foo" from the local host to a remote host's directory "bar"
$ scp -r foo your_username@remotehost.edu:/some/remote/directory/bar
 Copy the file "foobar.txt" from remote host "rh1.edu" to remote host "rh2.edu"
$ scp your_username@rh1.edu:/some/remote/directory/foobar.txt/
your_username@rh2.edu:/some/remote/directory/

20.sftp command :
 sftp is an interactive file transfer program which performs all operations over an encrypted
(ssh) transport.
 You can use SFTP from the command line on UNIX and Mac OS X computers. To start an
SFTP session, at the command prompt, enter:
sftp username@host
 For example, if your username is dvader, to connect to your account on the host
empire.gov, enter:
sftp dvader@empire.gov
Enter your password when prompted.
Some standard commands for command-line SFTP include:

 cd: Change the directory on the remote computer.


 chmod: Change the permissions of files on the remote computer.
 chown: Change the owner of files on the remote computer.
 exit (or quit) : Close the connection to the remote computer and exit SFTP.
 get : Copy a file from the remote computer to the local computer.
 help (or ?): Get help on the use of SFTP commands.
 lcd : Change the directory on the local computer.
 lls : List the contents of the current directory on the local computer.
 lmkdir : Create a directory on the local computer.
 ln (or symlink): Create a symbolic link for a file on the remote computer.
 lpwd : Show the current directory (present working directory) on the local
computer.
 ls (or dir): List the contents of the current directory on the remote computer.
 lumask: Change the local umask value.
 mkdir : Create a directory on the remote computer.
 put : Copy a file from the local computer to the remote computer.
 pwd : Show the current directory (present working directory) on the remote
computer.
 rm: Delete a file on the remote computer.
 rmdir: Remove a directory on the remote computer (the directory usually has to be
empty).
 version: Display the SFTP version.
 ! : Exit to the UNIX shell prompt, where you can enter commands. To get back to
SFTP, enter exit. If you combine! With a command (e.g.! pwd), SFTP will execute the
command without dropping you to the UNIX prompt.
 Example of sftp session:
 sftp gl.umbc.edu
(Computer now requests username and password information.)
 cd project/data
(On the remote machine, move to the project/data subdirectory.)
 ls
(Get a list of the files in the current remote directory.)
 get oldstuff.txt
(Copy the file oldstuff.txt from the remote directory.)
 get folder/olderstuff.txt
(Also copy the file olderstuff.txt, which is in a subdirectory on the remote
machine.)
 lcd..
(Move up one level in the local directory.)
 lls
(Get a directory of the current local directory.)
 put newstuff.txt currentstuff.txt
(Copy the local file newstuff.txt to the remote directory, but rename the
copy currentstuff.txt.)
 quit

21.ssh command :

SSH client utility in UNIX server is used to logging into a remote host and executes commands
on the remote machine. The ssh command provides a secure connection between two hosts
over an insecure network.

Example:

 > ssh username@remote-server


username@remote-server password:
 ssh user@remote-host "ls test"

22. vi Editor :

The vi editor (short for visual editor) is a screen editor which is available on almost all UNIX
systems.

Starting vi:

>vi filename: It will open the file in vi editor. If it doesn’t exist this will create a new one.

 vi has two modes: the command mode and the insert mode.
 You can type i to enter the insert mode. When you are in insert mode the same letters
of the keyboard will type or edit text.
 If you wish to leave insert mode and return to the command mode, hit the ESC key. vi
always starts out in command mode

Cursor Movement:

 h: left one space


 l: right one space
 j: down one space
 k:up one space

If you move the cursor as far as you can in any direction, you may see a screen flash or
hear a beep.

Moving among Words and Lines:


 w: moves the cursor forward one word
 b : moves the cursor backward one word (if in the middle of a word, b will move you to
the beginning of the current word).
 e: moves to the end of a word.

 We can use these commands with a number for greater movement.


 For example, 5w would move you forward five words. 12b would move you backwards
twelve words.
 You can also use numbers with the commands mentioned earlier. For example, 5j would
move you down 5 characters.

 Shortcuts: The $ key will move you to the end of a line, while the 0 will move you quickly to
the beginning of a line.

Screen Movement:
 H: moves the cursor to the top line of the screen.
 M: moves the cursor to the middle line of the screen.
 L: moves the cursor to the last line of the screen.

Deleting (or Cutting) Characters, Words, and Lines:


To delete a character, first place your cursor on that character. Then, you may use any of the
following commands:

 x deletes the character under the cursor.


 X deletes the character to the left of your cursor.
 dw deletes from the character selected to the end of the word.
 dd deletes all the current line.
 D deletes from the current character to the end of the line.
Preceding the command with a number will delete multiple characters. For example, 10x
will delete the character selected and the next 9 characters; 10X will delete the 10
characters to the left of the currently selected character. The command 5dw will delete 5
words, while 4dd deletes four lines.

Pasting Text using put:

 Often, when you delete or cut text, you may wish to reinsert it in another location of
the document.
 The Put command will paste in the last portion of text that was deleted since
deleted text is stored in a buffer.
 To use this command, place the cursor where you wish the deleted text to appear.
Then use p to reinsert the text.
 If you are inserting a line or paragraph use the lower case p to insert on the line
below the cursor or upper case P to place in on the line above the cursor.

Copying Text with Yank:


 yw copies a word into a buffer (7yw copies 7 words)
 yy copies a line into a buffer (3yy will copy 3 lines)

Once the desired text is yanked, place the cursor in the spot in which you wish to insert the text
and then use the put command (p for line below or P for line above) to insert the contents of
the buffer.

Closing and Saving Files

When you edit a file in vi, you are actually editing a copy of the file rather than the original. The
following sections describe methods you might use when closing a file, quitting vi, or both.

Quitting and Saving a File

The command ZZ (notice that it is in uppercase) will allow you to quit vi and save the edits
made to a file. You will then return to a UNIX prompt. Note that you can also use the following
commands:

:w to save your file but not quit vi (this is good to do periodically in Case of machine
crash!).

:q to quit if you haven't made any edits.

:wq to quit and save edits (basically the same as ZZ).

Quitting without Saving Edits


Sometimes, when you create a mess (when you first start using vi this is easy to do!) you may
wish to erase all edits made to the file and either start over or quit. To do this, you can choose
from the following two commands:

:e! Reads the original file back in so that you can start over.

:q! Wipes out all edits and allows you to exit from vi.

You might also like