You are on page 1of 58

UNIX / LINUX COMMANDS LIST

Find Command Grep Command Sed Command ls Command Copy (cp) Command Cut Command Crontab Command SCP Command WC Command SSH Command Kill Command FTP (File Transfer Protocol) Command Tar Command Sort Command Zip Command Top Command Chmod Command Translate (tr) Command Uniq Command Tail Command Head Command

WHAT IS A UNIX OPERATING SYSTEM


Unix is a multi-tasking, multi-user operating system. It is a layer between the hardware and the applications that run on the computer. It has functions which manage the hardware and applications. The structure of Unix operating system can be divided into three parts.

Kernel is the core part of Unix which interacts with the hardware for low level functions. Shell is the outer unit of Unix which interacts with the user to perform the functions. File System.

UNIX FILE STRUCTURE (FILE TREE)


The Unix file structure is organized in a reverse tree structure manner. The following figure shows a typical organization of files in Unix system.

The diagram looks like any upside-down tree. The slash (/) indicates the root directory. Names like etc, usr, local are directories and science.txt is a file. The regular files in Unix are the leaves in a tree structure.

DIFFERENT TYPES OF UNIX FILES


There are mainly three types of Unix files. They are

Regular files Directories Special or Device files Regular Files Regular files hold data and executable programs. Executable programs are the commands (ls) that you enter on the prompt. The data can be anything and there is no specific format enforced in the way the data is stored. The regular files can be visualized as the leaves in the UNIX tree.

Directories Directories are files that contain other files and sub-directories. Directories are used to organize the data by keeping closely related files in the same place. The directories are just like the folders in windows operating system. The kernel alone can write the directory file. When a file is added to or deleted from this directory, the kernel makes an entry. A directory file can be visualized as the branch of the UNIX tree. Special Or Device Files These files represent the physical devices. Files can also refer to computer hardware such as terminals and printers. These device files can also refer to tape and disk drives, CD-ROM players, modems, network interfaces, scanners, and any other piece of computer hardware. When a process writes to a special file, the data is sent to the physical device associated with it. Special files are not literally files, but are pointers that point to the device drivers located in the kernel. The protection applicable to files is also applicable to physical devices.

File Permission
File ownership is an important component of UNIX that provides a secure method for storing files. Every file in UNIX has the following attributes:

Owner permissions: The owner's permissions determine what actions the owner of the file can perform on the file. Group permissions: The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file. Other (world) permissions: The permissions for others indicate what action all other users can perform on the file.

File Access Modes: The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions, which are described below: 1. Read: Grants the capability to read ie. view the contents of the file. 2. Write: Grants the capability to modify, or remove the content of the file. 3. Execute: User with execute permissions can run a file as a program.

FIND COMMAND IN UNIX AND LINUX EXAMPLES


Find is one of the powerful utility of Unix (or Linux) used for searching the files in a directory hierarchy. The syntax of find command is

find [pathnames] [conditions]

Let see some practical exercises on using find command. 1. How to run the last executed find command?

!find

This will execute the last find command. It also displays the last find command executed along with the result on the terminal. 2. How to find for a file using name?

find -name "sum.java" ./bkp/sum.java ./sum.java

This will find all the files with name "sum.java" in the current directory and sub-directories. 3. How to find for files using name and ignoring case?

find -iname "sum.java" ./SUM.java ./bkp/sum.java

This will find all the files with name "sum.java" while ignoring the case in the current directory and sub-directories.

4. How to find for a file in the current directory only?

find -maxdepth 1 -name "sum.java" ./sum.java

This will find for the file "sum.java" in the current directory only 5. How to find for files containing a specific word in its name?

find -name "*java*" ./SUM.java ./bkp/sum.java ./sum.java

It displayed all the files which have the word "java" in the filename 6. How to find for files in a specific directory?

find /etc -name "*java*"

This will look for the files in the /etc directory with "java" in the filename 7. How to find the files whose name are not "sum.java"?

find -not -name "sum.java" ./SUM.java ./bkp

This is like inverting the match. It prints all the files except the given file "sum.java".

8. How to limit the file searches to specific directories?

find -name "sum.java" ./tmp/sum.java ./bkp/var/tmp/files/sum.java ./bkp/var/tmp/sum.java ./bkp/var/sum.java

You can see here the find command displayed all the files with name "sum.java" in the current directory and sub-directories.

./sum.java

9. How to find the empty files in a directory?

find . -maxdepth 1 -empty ./empty_file

10. How to find the largest file in the current directory and sub directories

11. How to find the smallest file in the current directory and sub directories

12. How to find files based on the file type? a. Finding socket files

find . -type s

b. Finding directories

find . -type d

c. Finding hidden directories

find -type d -name ".*"

d. Finding regular files

find . -type f

e. Finding hidden files

find . -type f -name ".*"

13. How to find files based on the size? a. Finding files whose size is exactly 10M

find . -size 10M

b. Finding files larger than 10M size

find . -size +10M

c. Finding files smaller than 10M size

find . -size -10M

14. How to find the files which are modified after the modification of a give file.

find -newer "sum.java"

This will display all the files which are modified after the file "sum.java" 15. Display the files which are accessed after the modification of a give file.

find -anewer "sum.java"

16. Display the files which are changed after the modification of a give file.

find -cnewer "sum.java"

17. How to find the files based on the file permissions?

find . -perm 777

This will display the files which have read, write, and execute permissions. To know the permissions of files and directories use the command "ls -l".

18. Find the files which are modified within 30 minutes.

find . -mmin -30

19. Find the files which are modified within 1 day.

find . -mtime -1

20. How to find the files which are modified 30 minutes back

find . -not -mmin -30

22. Print the files which are accessed within 1 hour.

find . -amin -60

23. Print the files which are accessed within 1 day.

find . -atime -1

24. Display the files which are changed within 2 hours.

find . -cmin -120

25. Display the files which are changed within 2 days.

find . -ctime -2

GREP COMMAND IN UNIX AND LINUX EXAMPLES


Grep is the frequently used command in Unix (or Linux). Most of us use grep just for finding the words in a file. The power of grep comes with using its options and regular expressions. You can analyze large sets of log files with the help of grep command. Grep stands for Global search for Regular Expressions and Print. The basic syntax of grep command is grep [options] pattern [list of files] Let see some practical examples on grep command. 1. Running the last executed grep command This saves a lot of time if you are executing the same command again and again.

!grep

This displays the last executed grep command and also prints the result set of the command on the terminal. 2. Search for a string in a file This is the basic usage of grep command. It searches for the given string in the specified file.

grep "Error" logfile.txt

This searches for the string "Error" in the log file and prints all the lines that has the word "Error". 3. Searching for a string in multiple files.

grep "string" file1 file2 grep "string" file_pattern

This is also the basic usage of the grep command. You can manually specify the list of files you want to search or you can specify a file pattern (use regular expressions) to search for.

4. Case insensitive search The -i option enables to search for a string case insensitively in the give file. It matches the words like "UNIX", "Unix", "unix".

grep -i "UNix" file.txt

5. Specifying the search string as a regular expression pattern.

grep "^[0-9].*" file.txt

This will search for the lines which starts with a number. Regular expressions is huge topic and I am not covering it here. This example is just for providing the usage of regular expressions. 6. Checking for the whole words in a file. By default, grep matches the given string/pattern even if it found as a substring in a file. The -w option to grep makes it match only the whole words.

grep -w "world" file.txt

10. Searching for a sting in all files recursively You can search for a string in all the files under the current directory and sub-directories with the help -r option.

grep -r "string" *

11. Inverting the pattern match You can display the lines that are not matched with the specified search sting pattern using the -v option.

grep -v "string" file.txt

12. Displaying the non-empty lines You can remove the blank lines using the grep command.

grep -v "^$" file.txt

13. Displaying the count of number of matches. We can find the number of lines that matches the given string/pattern

grep -c "sting" file.txt

14. Display the file names that matches the pattern. We can just display the files that contains the given string/pattern.

grep -l "string" *

15. Display the file names that do not contain the pattern. We can display the files which do not contain the matched string/pattern.

grep -L "string" *

16. Displaying only the matched pattern. By default, grep displays the entire line which has the matched string. We can make the grep to display only the matched string by using the -o option.

grep -o "string" file.txt

17. Displaying the line numbers. We can make the grep command to display the position of the line which contains the matched string in a file using the -n option

grep -n "string" file.txt

18. Displaying the position of the matched string in the line The -b option allows the grep command to display the character position of the matched string in a file.

grep -o -b "string" file.txt

19. Matching the lines that start with a string The ^ regular expression pattern specifies the start of a line. This can be used in grep to match the lines which start with the given string or pattern.

grep "^start" file.txt

20. Matching the lines that end with a string The $ regular expression pattern specifies the end of a line. This can be used in grep to match the lines which end with the given string or pattern.

grep "end$" file.txt

SED COMMAND IN UNIX AND LINUX EXAMPLES


Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use sed to replace text. You can do many things apart from replacing text with sed. Here I will describe the features of sed with examples. Consider the below text file as an input.

>cat file.txt unix is great os. unix is opensource. unix is free os. learn operating system. unixlinux which one you choose.

SED COMMAND EXAMPLES

1. Replacing or substituting string Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.

>sed 's/unix/linux/' file.txt linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.

Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string. By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.

2. Replacing the nth occurrence of a pattern in a line. Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.

>sed 's/unix/linux/2' file.txt unix is great os. linux is opensource. unix is free os. learn operating system. unixlinux which one you choose.

3. Replacing all the occurrence of the pattern in a line. The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

>sed 's/unix/linux/g' file.txt linux is great os. linux is opensource. linux is free os. learn operating system. linuxlinux which one you choose.

4. Replacing from nth occurrence to all occurrences in a line. Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.

>sed 's/unix/linux/3g' file.txt unix is great os. unix is opensource. linux is free os. learn operating system. unixlinux which one you choose.

5. Changing the slash (/) delimiter You can use any delimiter other than the slash. As an example if you want to change the web url to another url as

>sed 's/http:\/\//www/' file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work. Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.

>sed 's_http://_www_' file.txt >sed 's|http://|www|' file.txt

6. Using & as the matched string There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string.

>sed 's/unix/{&}/' file.txt {unix} is great os. unix is opensource. unix is free os. learn operating system. {unix}linux which one you choose.

>sed 's/unix/{&&}/' file.txt {unixunix} is great os. unix is opensource. unix is free os. learn operating system. {unixunix}linux which one you choose.

7. Using \1,\2 and so on to \9 The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word "unix" in a line with twice as the word like "unixunix" use the sed command as below.

>sed 's/\(unix\)/\1\1/' file.txt unixunix is great os. unix is opensource. unix is free os. learn operating system. unixunixlinux which one you choose.

The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the words "unixlinux" as "linuxunix", the sed command is

>sed 's/\(unix\)\(linux\)/\2\1/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxunix which one you choose.

Another example is switching the first three characters in a line

>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt inux is great os. unix is opensource. unix is free os. aelrn operating system. inuxlinux which one you choose.

8. Duplicating the replaced line with /p flag

The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

>sed 's/unix/linux/p' file.txt linux is great os. unix is opensource. unix is free os. linux is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose. linuxlinux which one you choose.

9. Printing only the replaced lines Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

>sed -n 's/unix/linux/p' file.txt linux is great os. unix is opensource. unix is free os. linuxlinux which one you choose.

If you use -n alone without /p, then the sed does not print anything.

10. Running multiple sed commands. You can run multiple sed commands by piping the output of one sed command as input to another

sed command.

>sed 's/unix/linux/' file.txt| sed 's/os/system/' linux is great system. unix is opensource. unix is free os. learn operating system. linuxlinux which one you chosysteme.

Sed provides -e option to run multiple sed commands in a single sed command. The above output can be achieved in a single sed command as shown below.

>sed -e 's/unix/linux/' -e 's/os/system/' file.txt linux is great system. unix is opensource. unix is free os. learn operating system. linuxlinux which one you chosysteme.

11. Replacing string on a specific line number. You can restrict the sed command to replace the string on a specific line number. An example is

>sed '3 s/unix/linux/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system. linuxlinux which one you choose.

The above sed command replaces the string only on the third line.

13. Replace on a lines which matches a pattern.

You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then only the sed command looks for the string to be replaced and if it finds, then the sed command replaces the string.

>sed '/linux/ s/unix/centos/' file.txt unix is great os. unix is opensource. unix is free os. learn operating system.

Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos". 14. Deleting lines. You can delete the lines a file by specifying the line number or a range or numbers.

>sed '2 d' file.txt >sed '5,$ d' file.txt

15. Duplicating lines You can make the sed command to print each line of a file two times.

>sed 'p' file.txt

16. Sed as grep command You can make sed command to work as similar to grep command.

>grep 'unix' file.txt >sed -n '/unix/ p' file.txt

Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that

has the pattern. You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).

>grep -v 'unix' file.txt >sed -n '/unix/ !p' file.txt

The ! here inverts the pattern match. 17. Add a line after a match. The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found.

>sed '/unix/ a "Add a new line"' file.txt unix is great os. unix is opensource. unix is free os. "Add a new line" learn operating system. unixlinux which one you choose. "Add a new line"

18. Add a line before a match The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.

>sed '/unix/ i "Add a new line"' file.txt "Add a new line" unix is great os. unix is opensource. unix is free os.

learn operating system. "Add a new line" unixlinux which one you choose.

19. Change a line The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line.

>sed '/unix/ c "Change line"' file.txt "Change line" learn operating system. "Change line"

20. Transform like tr command The sed command can be used to convert the lower case letters to upper case letters by using the transform "y" option.

>sed 'y/ul/UL/' file.txt Unix is great os. Unix is opensoUrce. Unix is free os. Learn operating system. UnixLinUx which one yoU choose.

Here the sed command transforms the alphabets "ul" into their uppercase format "UL"

UNIX SED COMMAND TO DELETE LINES IN FILE - 15 EXAMPLES

Sed Command to Delete Lines: Sed command can be used to delete or remove specific lines which matches a given pattern or in a particular position in a file. Here we will see how to delete lines using sed command with various examples. The following file contains a sample data which is used as input file in all the examples:

> cat file linux unix fedora debian ubuntu

SED COMMAND TO DELETE LINES - BASED ON POSITION IN FILE


In the following examples, the sed command removes the lines in file that are in a particular position in a file. 1. Delete first line or header line The d option in sed command is used to delete a line. The syntax for deleting a line is:

> sed 'Nd' file

Here N indicates Nth line in a file. In the following example, the sed command removes the first line in a file.

> sed '1d' file unix fedora

debian ubuntu

2. Delete last line or footer line or trailer line The following sed command is used to remove the footer line in a file. The $ indicates the last line of a file.

> sed '$d' file linux unix fedora debian

3. Delete particular line This is similar to the first example. The below sed command removes the second line in a file.

> sed '2d' file linux fedora debian ubuntu

4. Delete range of lines The sed command can be used to delete a range of lines. The syntax is shown below:

> sed 'm,nd' file

Here m and n are min and max line numbers. The sed command removes the lines from m to n in the file. The following sed command deletes the lines ranging from 2 to 4:

> sed '2,4d' file linux ubuntu

5. Delete lines other than the first line or header line Use the negation (!) operator with d option in sed command. The following sed command removes all the lines except the header line.

> sed '1!d' file linux

6. Delete lines other than last line or footer line

> sed '$!d' file ubuntu

7. Delete lines other than the specified range

> sed '2,4!d' file unix

fedora debian

Here the sed command removes lines other than 2nd, 3rd and 4th. 8. Delete first and last line You can specify the list of lines you want to remove in sed command with semicolon as a delimiter.

> sed '1d;$d' file unix fedora debian

9. Delete empty lines or blank lines

> sed '/^$/d' file

The ^$ indicates sed command to delete empty lines. However, this sed do not remove the lines that contain spaces.

SED COMMAND TO DELETE LINES - BASED ON PATTERN MATCH


In the following examples, the sed command deletes the lines in file which match the given pattern. 10. Delete lines that begin with specified character

> sed '/^u/d' file

linux fedora debian

^ is to specify the starting of the line. Above sed command removes all the lines that start with character 'u'. 11. Delete lines that end with specified character

> sed '/x$/d' file fedora debian ubuntu

$ is to indicate the end of the line. The above command deletes all the lines that end with character 'x'. 12. Delete lines which are in upper case or capital letters

> sed '/^[A-Z]*$/d' file

13. Delete lines that contain a pattern

> sed '/debian/d' file

14. Delete lines starting from a pattern till the last line

> sed '/fedora/,$d' file linux unix

Here the sed command removes the line that matches the pattern fedora and also deletes all the lines to the end of the file which appear next to this matching line. 15. Delete last line only if it contains the pattern

> sed '${/ubuntu/d;}' file linux unix

Here $ indicates the last line. If you want to delete Nth line only if it contains a pattern, then in place of $ place the line number. Note: In all the above examples, the sed command prints the contents of the file on the unix or linux terminal by removing the lines. However the sed command does not remove the lines from the source file. To Remove the lines from the source file itself, use the -i option with sed command.

> sed -i '1d' file

If you dont wish to delete the lines from the original source file you can redirect the output of the sed command to another file.

sed '1d' file > newfile

SED COMMAND TO PRINT MATCHING LINES - UNIX / LINUX


We use sed command mostly to do operations like search and replace. We can also use the sed command just like a grep to print only the matching lines. The syntax of sed command to print matching lines is shown below:

sed -n '/pattern/p' filename

The syntax of sed command to print lines using line addresses is:

sed -n 'Np' filename

Create the following file in your unix or linux machine:

unix virtual server linux virtual hosting fedora os

1. Display lines matching a pattern The following examples prints the lines that contain the pattern virtual using sed and grep commands.

> grep 'virtual' filename > sed -n '/virtual/p' filename

2. Print lines with specific line addresses.

The below sed command displays the line with number 2:

sed -n '2p' filename

LS COMMAND IN UNIX AND LINUX EXAMPLES


ls is the most widely used command in unix or linux. ls command is used to list the contents of a directory. Learn the power of ls command to make your life easy. The syntax of ls command is

ls [options] [pathnames]

1. Write a unix/linux ls command to display the hidden files and directories? To display the hidden files and directories in the current directory use the -a option of the ls command.

> ls -a . .. documents .hidden_file sum.pl

Hidden files are the one whose name starts with dot (.). The las -a displays the current directory (.) and parent directory (..) also. If you want to exclude the current directory, parent directory, then use -A option.

> ls -A documents .hidden_file sum.pl

2. Write a unix/linux ls command to classify the files with special characters The -F option to ls command classifies the files. It marks the

Directories with trailing slash (/) Executable files with trailing asterisk (*) FIFOs with trailing vertical bar (|) Symbolic links with trailing at the rate sign (@) Regular files with nothing

> ls -F documents/ sum.pl link@

3. Write a unix/linux ls command to print each file in a separate line? The -1 option to the ls command specifies that each file should be displayed on a separate line

> ls -1 documents sum.pl

4. Write a unix/linux ls command to display the inode number of file? In some cases, you want to know the inode number of a file. Use -i option to the ls command to print the inode number of a file.

> ls -i1 10584066 documents 3482450 sum.pl

5. Write a unix/linux ls command to display complete information about the files? The -l option provides lots of information about the file type, owner, group, permissions, file size, last modification date.

> ls -l total 16 drwxr-xr-x 2 matt db 4096 Jan 30 23:08 documents -rw-r--r-- 1 matt db 49 Jan 31 01:17 sum.pl

The first character indicates the type of the file. - for normal file, d for directory, l for link file and s for socket file The next 9 characters in the first field represent the permissions. Each 3 characters refers the read (r), write (w), execute (x) permissions on owner, group and others. - means no permission. The second field indicates the number of links to that file. The third field indicates the owner name. The fourth field indicates the group name. The fifth field represents the file size in bytes. The sixth field represents the last modification date and time of the file. And finally the seventh field is the name of the file. 6. Write a unix/linux ls command to sort the files by their modification time? The -t option allows the ls command to sort the files in descending order based on the modification time.

> ls -t1 sum.pl documents

7. Write a unix/linux ls command to sort the files in ascending order of modification time? The -r option reverses the order of the files displayed. Combine the -t and -r options to sort the files in ascending order.

> ls -rt1 documents sum.pl

8. Write a unix/linux ls command to print the files recursively? So far the ls command prints the files in the current directory. Use the -R option to recursively print the files in the sub-directories also.

> ls -R .:documents sum.pl ./documents: file.txt

9. Write a unix/linux ls command to print the files in a specific directory? You can pass a directory to the ls command as an argument to print for the files in it.

> ls /usr/local/bin

10. Write a unix/linux ls command to display files in columns?

The -x option specifies the ls command to display the files in columns.

> ls -x

COPY (CP) FILE AND DIRECTORY EXAMPLES


Copy (cp) is the frequently used command in Unix (or Linux). The cp Command is used to copy the files from one directory to another directory. The cp command can also be used to copy the directories also. The syntax of cp command is

cp [options] source destination

Examples of cp Command 1. Write a unix/linux cp command to copy file in to a directory? The basic usage of cp command is to copy a file from the current directory to another directory.

cp sum.pl tmp/

The cp command copies the file sum.pl into the tmp directory. The cp command does not remove the source file. It just copies the file into a new location. If a file with the same name as the source exists in the destination location, then by default the cp command overwrites that new file 2. Write a unix/linux cp to prompt for user before overwriting a file ( Interactive cp command)? The -i option to the cp command provides the ability to prompt for a user input whether to overwrite the destination file or not.

> cp sum.pl tmp/ cp: overwrite `tmp/sum.pl'?

If you enter y, then the cp command overwrites the destination file, otherwise the cp command does not copy the file.

3. Write a unix/linux cp command to copy multiple files in to a new directory? You can specify multiple files as the source and can copy to the new location.

cp log.dat bad.dat tmp/

The cp command copies the log.dat, bad.dat files in the current directory to the tmp directory.

4. Write a unix/linux cp command to do a Regular expression copy? You can copy a set of files by specifying a regular expression pattern.

cp *.dat tmp/

Here the cp command copies all the files which has "dat" as suffix to the destination directory. 5. Write a unix/linux cp command to copy a file in to the current directory? You can copy a file from a different directory to the current directory.

cp /usr/local/bin/multiply.sh .

Here the cp command copies the multiply.sh file in the /usr/local/bin directory the current directory. The dot (.) indicates the current directory. 6. Write a unix/linux cp command to copy all the files in a directory? The cp command can be used to copy all the files in directory to another directory.

cp docs/* tmp/

This command copies all the files in the docs directory to the tmp directory. 7. Write a unix/linux cp command to copy files from multiple directories? You can copy the files from different directories into a new location.

cp docs/* scripts/* tmp/

The command copies the files from docs and script directories to the destination directory tmp.

8. Write a unix/linux cp command to Copy a directory. You can recursively copy a complete directory and its sub directory to another location using the cp command

cp -r docs tmp/

This copies the complete directory docs into the new directory tmp 9. Write a unix/linux cp command to Forcibly copy a file with -f option? You can force the cp command to copy an existing destination file even it cannot be opened.

cp -f force_file.txt /var/tmp/

CUT COMMAND IN UNIX ( LINUX) EXAMPLES


Cut command in unix (or linux) is used to select sections of text from each line of files. You can use the cut command to select fields or columns from a line by specifying a delimiter or you can select a portion of text by specifying the range or characters. Basically the cut command slices a line and extracts the text. Unix Cut Command Example We will see the usage of cut command by considering the below text file as an example

> cat file.txt unix or linux os is unix good os is linux good os

1. Write a unix/linux cut command to print characters by position? The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command

cut -c4 file.txt x u l

The above cut command prints the fourth character in each line of the file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example

cut -c4,6 file.txt

This command prints the fourth and sixth character in each line.

2.Write a unix/linux cut command to print characters by range? You can print a range of characters in a line by specifying the start and end position of the characters.

cut -c4-7 file.txt x or unix

linu

The above cut command prints the characters from fourth position to the seventh position in each line. To print the first six characters in a line, omit the start position and specify only the end position.

cut -c-6 file.txt unix o is uni is lin

To print the characters from tenth position to the end, specify only the start position and omit the end position.

cut -c10- file.txt inux os ood os good os

If you omit the start and end positions, then the cut command prints the entire line.

cut -c- file.txt

3.Write a unix/linux cut command to print the fields using the delimiter? You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.

cut -d' ' -f2 file.txt or

This command prints the second field in each line by treating the space as delimiter. You can print more than one field by specifying the position of the fields in a comma delimited list.

cut -d' ' -f2,3 file.txt or linux

The above command prints the second and third field in each line. Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command. 4. Write a unix/linux cut command to display range of fields? You can print a range of fields by specifying the start and end position.

cut -d' ' -f1-3 file.txt

The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position.

cut -d' ' -f-3 file.txt

To print the fields from second fields to last field, you can omit the last field position.

cut -d' ' -f2- file.txt

5. Write a unix/linux cut command to display the first field from /etc/passwd file? The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is

cut -d':' -f1 /etc/passwd

6. The input file contains the below text

> cat filenames.txt logfile.dat sum.pl add_int.sh

Using the cut command extract the portion after the dot. First reverse the text in each line and then apply the command on it.

rev filenames.txt | cut -d'.' -f1

ADD JOB TO CRON (CRONTAB COMMAND EXAMPLES) - UNIX / LINUX TUTORIALS


Unix or Linux operating system provides a feature for scheduling the jobs. You can setup command or scripts which will run periodically at the specified time. The Crontab is command used to add or remove jobs from the cron. The cron service is a daemon runs in the background and checks for /etc/crontab file, /etc/con.*/ directories and /var/spool/cron/ directory for any scheduled jobs. Each user has a separate /var/spool/cron/crontab file. Users are not allowed directly to modify the files. The crontab command is used for setting up the jobs in the cron.

The format of crontab command is

* * * * * command to be executed

You can easily remember this command in the below format

MI HH DOM MON DOW command

The field descriptions of the crontab are explained below:

MI HH

: Minutes : Hours

from 0 to 59 from 0 to 23

DOM : Day of month from 0 to 31 MON : Months DOW : Day of week from 1 to 12 from 0 to 7 (0 or 7 represents Sunday)

Command: Any command or script to be scheduled

Let see the usage of crontab command with examples. 1. List crontab entries You can list out all the jobs which are already scheduled in cron. Use "crontab -l" for listing the jobs.

crontab -l

0 0 * * *

/usr/local/bin/list_unix_versions.sh

The above contab command displays the cron entries. Here the shell script for listing the unix versions (list_unix_version.sh) is scheduled to run daily at midnight. 2. List crontab entries of other users To list the corntab entries of other user in the unix, use the -u option with crontab. The syntax is shown below:

crontab -u username -l

3. Removing all crontab entries You can un-schedule all the jobs by removing them from the crontab. The syntax for removing all the crontab entries is

crontab -r

For removing other users crontab entries: crontab -u username -r

4. Editing the crontab You can edit the crontab and add a new job to it. You can also remove an existing job from the crontab. Use the -e option for editing the crontab.

crontab -e

For editing other users crontab entries: crontab -u username -e

This will open a file in VI editor. Now use the VI commands for adding, removing the jobs and for saving the crontab entries. 5. Schedule a job to take oracle backup on every Sunday at midnight Edit crontab using "crontab -e" and append the following entry in the file.

0 0 * * 0 /usr/local/bin/oracle_backup.sh

6. Schedule a job to run every six hours in a day You can schedule a job to run more than once in a day. As an example the following crontab entry takes the mysql backup more than once in a day.

0 0,6,12,18 * * * /usr/bin/mysql_backup.sh

Here the list 0,6,12,18 indicates midnight, 6am, 12pm and 6pm respectively.

7. Schedule job to run for the first 15 days of the month. You can schedule a job by specifying the range of values for a field. The following example takes the sql server backup daily at midnight for the first 15 days in a month.

0 0 * 1-15 * /usr/bin/sql_server_backup.sh

8. Schedule job to run every minute. The following crontab command runs the command to send emails to group of users for every

minute.

* * * * * /bin/batch_email_send.sh

9. Taking backup of cron entries Before editing the cron entries, it is good to take backup of the cron entries. So that even if you do mistake you can get back those entries from the backup.

crontab -l > /var/tmp/cron_backup.dat

10. Restoring the cron entries You can restore the cron entries from the backup as

crontab cron_backup.dat

.sh > /dev/null 2>&1

Note: you cannot schedule a job to run at seconds level as the minimum allowed scheduling is at minute level.

SCP COMMAND EXAMPLES


SCP stands for secure copy is used to copy data (files or directories) from one unix or linux system to another unix or linux server. SCP uses secured shell (ssh) to transfer the data between the remote hosts. The features of SCP are:

Copies files within in the same machine Copies files from local machine to remote machine. Copies files from remote machine to local machine. Copies files between two different remote servers.

SCP Command Syntax: The syntax of SCP command is

scp [Options] [[User@]From_Host:]Source_File [[User@]To_Host:][Destination_File]

Each element of the scp command is explained in detail below:

User is the one who have the permissions to access the files and directories. User should have read permissions if it is a source and write permissions if it is the destination. From_Host: hostname or Ip address where the source file or directory resides. This is optional if the from host is the host where you are running the scp command. Source_File: Files or directories to be copied to the destination. To_Host: Destination host where you want to copy the files. You can omit this when you want to copy the files to the host where you are issuing the scp command. Destination_File: Name of the file or directory in the target host. SCP Command Options: The important SCP command options are listed below:

-r : Recursively copies the contents of source files or directories. -p : Preserves the access time, modification time, permissions of the source files in the destination. -q : Progress bar in not displayed -v : verbose mode. Displays debugging messages. -P : copy files using the specified port number.

SCP Command Examples: Let see the examples of scp command in unix or linux system. 1. Copying with in the same system You can use the scp command just like the cp command to copy files from one directory to another directory.

scp Unix-storage.dat /var/tmp/

This command copies the file unix-storage.dat from current directory to the /var/tmp directory. 2. Copy file from local host to remote server This is most frequently used operation to transfer files in unix system.

scp filename user@remotehost:/remote/directory/

This command connects to the remote host and copies the specified file to the /remote/directory/. 3. Copy files from remote host to local server. This operation is used when taking backup of the files in remote server.

scp user@remotehost:/usr/backup/oracle_backup.dat .

This command copies the oracle backup file in the remote host to the current directory.

4. Copying files between two remote servers The scp command can also be used to copy files between two remote hosts.

scp source_user@source_remote_host:/usr/bin/mysql_backup.sh target_user@target_remote_host:/var/tmp/

The above command copies the mysql bakup shell script from the source remote host the /var/tmp directory of target remote host. 5. Copying a directory.

To copy all the files in a directory, use the -r option with the scp command. This makes the scp command to copy the directory recursively.

scp -r directory user@remotehost:/var/tmp/

WC COMMAND EXAMPLES - COUNT OF LINES, WORDS, CHARACTERS UNIX / LINUX


WC command in unix or linux is used to find the number of lines, words and characters in a file. The syntax of wc command is shown below:

wc [options] filenames

You can use the following options with the wc command.

-l : Prints the number of lines in a file. -w : prints the number of words in a file. -c : Displays the count of bytes in a file. -m : prints the count of characters from a file. -L : prints only the length of the longest line in a file.

Let see how to use the wc command with few examples. Create the following file in your unix or linux operating system.

> cat unix_wc.bat Oracle Storage unix distributed system linux file server debian server Oracle backup server

WC Command Examples: 1. Printing count of lines This is the most commonly used operation to find the number of lines from a file. Run the below command to display the number of lines:

wc -l unix_wc.bat

Here in the output, the first field indicates count and second field is the filename 2. Displaying the number of words.

Just use the -w option to find the count of words in a file. This is shown below:

wc -w unix_wc.bat

3. Print count of bytes, count of characters from a file We can use the -c and -m options to find the number of bytes and characters respectively in a file.

> wc -c unix_wc.bat > wc -m unix_wc.bat

4. Print the length of longest line The -L option is used to print the number of characters in the longest line from a file.

wc -L unix_wc.bat

5. Print count of lines, words and characters. If you dont specify any option to the wc command, by default it prints the count of lines, words and characters. This is shown below:

wc unix_wc.bat

SSH COMMAND EXAMPLES - UNIX / LINUX


SSH client utility in unix or linux server is used to logging into a remote host and execute commands on the remote machine. The rlogin and rsh commands can also be used to login into the remote machine. However these are not secure. The ssh command provides a secure connection between two hosts over a insecure network. The syntax ssh command is

ssh [-l username] hostname | user@remote-hostname [command]

Let see the examples of ssh command. SSH Command Examples: 1. Logging to a remote server You can login to a remote server from the local host as shown below:

localhost:[~]> ssh -l username remote-server username@remote-server password: remote-server:[~]>

Alternatively you can use the below ssh command for connecting to remote host:

localhost:[~]> ssh username@remote-server username@remote-server password: remote-server:[~]>

Note: If you are logging for the first time, then it will prints a message that host key not found and you can give yes to continue. The host key of the remote server will be cached and added to the .ssh2/hostkeys directory in your home directory. From second time onwards you just need to enter the password. 2. Logging out from remote server Simply enter the exit command on the terminal to close the connection. This is shown below:

remote-server:[~]>exit logout Connection to remote-server closed. localhost:[~]>

3. Running remote commands from local host Sometimes it is necessary to run the unix commands on the remote server from the local host. An example is shown below:

localhost:[~]> ssh user@remote-host "ls test" online-backup.dat oracle-storage.bat unix-dedicated-server.txt

The ssh command connects to the remote host, runs the ls command, prints the output on the local host terminal and exits the connection from remote host. Let see whether the ls command actually displayed the correct result or not by connecting to the remote host.

localhost:[~]> ssh user@remote-host user@remotehost password: remotehost:[~]> cd test remotehost:[~/test]> ls online-backup.dat

4. Version of the SSH command We can find the version of SSH installed on the unix system using the -V option to the ssh. This is shown below:

> ssh -V OpenSSH_4.3p2, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008

6. Copying files between remote host and local host.

We can use the scp command to copy the files securely between the local host and remote host using the ssh authentication. To copy the file from local host to remote hosts /var/tmp/ directory, run the below scp command.

scp filename user@remote-host:/var/tmp/

To copy the file from remote hosts /usr/local/bin/ directory to local hosts current directory, run the below scp command.

scp user@remote-host:/usr/local/bin/add.sh .

KILL COMMAND EXAMPLES IN UNIX / LINUX


The Kill command in unix or linux operating system is used to send a signal to the specified process or group. If we dont specify any signal, then the kill command passes the SIGTERM signal. We mostly use the kill command for terminating or killing a process. However we can also use the kill command for running a stopped process. The syntax of kill command is

kill [-s signal] pid

kill -l

The options to the kill command are:


pid : list of process that kill command should send a signal -s signal : send the specified signal to the process -l : list all the available signals.

Let see some of the useful kill command examples in unix or linux system. Kill Command Examples: 1. Listing all the signal names. Run the kill command with -l option to list all the available signal names.

> kill -l HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU XFSZ WAITING LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX

Some the important signals which we use in our daily usage ar listed below:

Number Signal Name 0 1 2 SIGNULL SIGHUP SIGNINT

Description Used to check access to the process id Hup signal. Terminates the process. Interrupt signal. Terminating the process

3 9 24 26

SIGQUIT SIGKILL SIGSTOP SIGCONT

Quit signal. Terminate process with core dump Forcibly killing a process Pausing the process Runs a stopped process

To know more about a signal, check in man pages. To know about the signal 9, run the below man command:

man 5 signal

2. Getting the process id To know the process id of a process running in the unix system, use the ps command as

ps -aef root .... .... 4529 657 0 Jul 20 ? 0:06 /usr/local/sbin/sshd -R

The second field in the output is the process Id. Here the /usr/local/sbin/sshd -R is running with the process id 4529. 3. Killing a process. To kill processes simply pass the process id to the kill command. This is shown below:

kill 4529

4. Forcefully killing a process.

Use the -9 option with the kill command to kill a process force fully. The following kill command terminates the process forcefully:

kill -9 1567 kill -SIGKILL 1567 kill -KILL 1567 kill -s SIGKILL 1567 kill -s KILL 1567

FTP (FILE TRANSFER PROTOCOL) COMMAND EXAMPLES


The FTP (file transfer program) utility is used to transfer files between a local machine and remote network machine Using the File Transfer protocol. In simple terms it transfers / copies files between two computers. You can transfer files between unix systems and also non-unix systems like windows operating system using FTP. The FTP command is simple to use and easy to learn. Let see useful examples of FTP command in detail.

FTP Command Examples: If you are using windows operating system, open the command prompt and practice the below FTP commands. If you are using unix or linux operating systems, just simply type the ftp command on the terminal. 1. Connecting to Remote Host First you need to connect to a remote host before doing any operations. You can use any one of the following methods to connect to a remote host. First method is

> ftp remote-server-name connected to remote-server-name User-Name: Password: ftp>

Once the ftp connects to the remote server name, it will prompt you to enter the user name and password. After successful login, your terminal or prompt changes to "ftp>". Another method is to use the open option with ftp command. This is shown below:

>ftp ftp>open remote-server-name connected to remote-server-name User-Name: Password: ftp>

If the ftp command fails to connect to the remote server, then you will get the below error:

ftp: connect: Connection refused

2. Copy file from remote machine to local machine. The get option is used to download or transfer a file from the remote system to the local system.

ftp> get windows-cleveland.bat

This will download the specified file (windows-cleveland.bat) from the remote systems current directory. 3. Copying multiple files from remote machine to local machine. You can use the mget to transfer multiple files from the remote host to local host.

ftp>mget *.png

This will download all the png images to the local machine. 4. Transferring file from local server to remote server The put option is used to copy the file from the local host to the remote host.

ftp>put linux-virtual-server.rpm

This command puts the rpm file into the remote machine. 6. Executing commands in remote machine. After connecting to the remote network machine using the ftp, you can run commands like ls to list the files, cd to change directory and many more.

ftp> ls

This will list the files and directories in the remote machines current directory.

7. Executing commands in local machine. Once you have connected to the remote host, to run the commands on local machine you need to exit from the ftp connection. Instead of this, there is a way to run commands on local host without exiting from the ftp connection. Use the ! symbol before the command you want to run.

ftp> !ls

Now this will list the files in the local machines current directory. 9. Deleting files on remote machine You can use the delete or mdelete to remove a single file or multiple files in the remote machine.

ftp>delete linux-dedicated-server.dat ftp>mdelete *.dat

10. Disconnecting from ftp connection.

Use the quit command to close the ftp connection.

ftp>quit

You might also like