Most frequently used LINUX commands with examples for oracle database administrator
Tar command with examples
1)tar : The tar command manipulates archives by writing files to, or retrieving files from an archive storage medium. The files used by the tar command are represented by the File parameter.
1. Creating an archive using tar command
Creating an uncompressed tar archive using option cvf
This is the basic command to create a tar archive.
$ tar cvf archive_name.tar dirname/
In the above command:
- c – create a new archive
- v – verbosely list files which are processed.
- f – following is the archive file name
Creating a tar gzipped archive using option cvzf
The above tar cvf option, does not provide any compression. To use a gzip compression on the tar archive, use the z option as shown below.
$ tar cvzf archive_name.tar.gz dirname/
- z – filter the archive through gzip
Note: .tgz is same as .tar.gz
Note: I like to keep the ‘cvf’ (or tvf, or xvf) option unchanged for all archive creation (or view, or extract) and add additional option at the end, which is easier to remember. i.e cvf for archive creation, cvfz for compressed gzip archive creation, cvfj for compressed bzip2 archive creation etc., For this method to work properly, don’t give – in front of the options.
Creating a bzipped tar archive using option cvjf
Create a bzip2 tar archive as shown below:
$ tar cvfj archive_name.tar.bz2 dirname/
- j – filter the archive through bzip2
gzip vs bzip2: bzip2 takes more time to compress and decompress than gzip. bzip2 archival size is less than gzip.
Note: .tbz and .tb2 is same as .tar.bz2
2. Extracting (untar) an archive using tar command
Extract a *.tar file using option xvf
Extract a tar file using option x as shown below:
$ tar xvf archive_name.tar
- x – extract files from archive
Extract a gzipped tar archive ( *.tar.gz ) using option xvzf
Use the option z for uncompressing a gzip tar archive.
$ tar xvfz archive_name.tar.gz
Extracting a bzipped tar archive ( *.tar.bz2 ) using option xvjf
Use the option j for uncompressing a bzip2 tar archive.
$ tar xvfj archive_name.tar.bz2
Note: In all the above commands v is optional, which lists the file being processed.
3. Listing an archive using tar command
View the tar archive file content without extracting using option tvf
You can view the *.tar file content before extracting as shown below.
$ tar tvf archive_name.tar
View the *.tar.gz file content without extracting using option tvzf
You can view the *.tar.gz file content before extracting as shown below.
$ tar tvfz archive_name.tar.gz
View the *.tar.bz2 file content without extracting using option tvjf
You can view the *.tar.bz2 file content before extracting as shown below.
$ tar tvfj archive_name.tar.bz2
4. Listing out the tar file content with less command
When the number of files in an archive is more, you may pipe the output of tar to less. But, you can also use less command directly to view the tar archive output, as explained in one of our previous article
5. Extract a single file from tar, tar.gz, tar.bz2 file
To extract a specific file from a tar archive, specify the file name at the end of the tar xvf command as shown below. The following command extracts only a specific file from a large tar file.
$ tar xvf archive_file.tar /path/to/file
Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.
$ tar xvfz archive_file.tar.gz /path/to/file
$ tar xvfj archive_file.tar.bz2 /path/to/file
6. Extract a single directory from tar, tar.gz, tar.bz2 file
To extract a single directory (along with it’s subdirectory and files) from a tar archive, specify the directory name at the end of the tar xvf command as shown below. The following extracts only a specific directory from a large tar file.
$ tar xvf archive_file.tar /path/to/dir/
To extract multiple directories from a tar archive, specify those individual directory names at the end of the tar xvf command as shown below.
$ tar xvf archive_file.tar /path/to/dir1/ /path/to/dir2/
Use the relevant option z or j according to the compression method gzip or bzip2 respectively as shown below.
$ tar xvfz archive_file.tar.gz /path/to/dir/
$ tar xvfj archive_file.tar.bz2 /path/to/dir/
7. Extract group of files from tar, tar.gz, tar.bz2 archives using regular expression
You can specify a regex, to extract files matching a specified pattern. For example, following tar command extracts all the files with pl extension.
$ tar xvf archive_file.tar --wildcards '*.pl'
Options explanation:
- –wildcards *.pl – files with pl extension
8. Adding a file or directory to an existing archive using option -r
You can add additional files to an existing tar archive as shown below. For example, to append a file to *.tar file do the following:
$ tar rvf archive_name.tar newfile
This newfile will be added to the existing archive_name.tar. Adding a directory to the tar is also similar,
$ tar rvf archive_name.tar newdir/
Note: You cannot add file or directory to a compressed archive. If you try to do so, you will get “tar: Cannot update compressed archives” error as shown below.
$ tar rvfz archive_name.tgz newfile
tar: Cannot update compressed archives
Try `tar --help' or `tar --usage' for more information.
9. Verify files available in tar using option -W
As part of creating a tar file, you can verify the archive file that got created using the option W as shown below.
$ tar cvfW file_name.tar dir/
If you are planning to remove a directory/file from an archive file or from the file system, you might want to verify the archive file before doing it as shown below.
$ tar tvfW file_name.tar
Verify 1/file1
1/file1: Mod time differs
1/file1: Size differs
Verify 1/file2
Verify 1/file3
If an output line starts with Verify, and there is no differs line then the file/directory is Ok. If not, you should investigate the issue.
Note: for a compressed archive file ( *.tar.gz, *.tar.bz2 ) you cannot do the verification.
Finding the difference between an archive and file system can be done even for a compressed archive. It also shows the same output as above excluding the lines with Verify.
Finding the difference between gzip archive file and file system
$ tar dfz file_name.tgz
Finding the difference between bzip2 archive file and file system
$ tar dfj file_name.tar.bz2
10. Estimate the tar archive size
The following command, estimates the tar file size ( in KB ) before you create the tar file.
$ tar -cf - /directory/to/archive/ | wc -c
20480
The following command, estimates the compressed tar file size ( in KB ) before you create the tar.gz, tar.bz2 files.
$ tar -czf - /directory/to/archive/ | wc -c
508
$ tar -cjf - /directory/to/archive/ | wc -c
Grep command with examples
First create the following demo_file that will be used in the examples below to demonstrate grep command.
$ cat demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
1. Search for the given string in a single file
The basic usage of grep command is to search for a specific string in the specified file as shown below.
Syntax:
grep "literal_string" filename
$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
2. Checking for the given string in multiple files.
Syntax:
grep "string" FILE_PATTERN
This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.
$ cp demo_file demo_file1
$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.
3. Case insensitive search using grep -i
Syntax:
grep -i "string" FILE
This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.
4. Match regular expression in files
Syntax:
grep "REGEX" filename
This is a very powerful feature, if you can use use regular expression effectively. In the following example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-between. i.e To search “lines[anything in-between]empty” in the demo_file.
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
From documentation of grep: A regular expression may be followed by one of several repetition operators:
- ? The preceding item is optional and matched at most once.
- * The preceding item will be matched zero or more times.
- + The preceding item will be matched one or more times.
- {n} The preceding item is matched exactly n times.
- {n,} The preceding item is matched n or more times.
- {,m} The preceding item is matched at most m times.
- {n,m} The preceding item is matched at least n times, but not more than m times.
5. Checking for full words, not for sub-strings using grep -w
If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out all the lines.
The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.
The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.
$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.
The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.
$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
6. Displaying lines before/after/around the match using grep -A, -B and -C
When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match.
Please create the following demo_text file for this example.
$ cat demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
* e - go to the end of the current word.
* E - go to the end of the current WORD.
* b - go to the previous (before) word.
* B - go to the previous (before) WORD.
* w - go to the next word.
* W - go to the next WORD.
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.1 Display N lines after match
-A is the option which prints the specified N lines after the match as shown below.
Syntax:
grep -A <N> "string" FILENAME
The following example prints the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
6.2 Display N lines before match
-B is the option which prints the specified N lines before the match.
Syntax:
grep -B <N> "string" FILENAME
When you had option to show the N lines after match, you have the -B option for the opposite.
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
6.3 Display N lines around match
-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.
$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
7. Highlighting the search using GREP_OPTIONS
As grep prints out lines from the file by the pattern / string you had given, if you wanted it to highlight which part matches the line, then you need to follow the following way.
When you do the following export you will get the highlighting of the matched searches. In the following example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown below.
When you do the following export you will get the highlighting of the matched searches. In the following example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown below.
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'
$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.
8. Searching in all files recursively using grep -r
When you want to search in all the files under the current directory and its sub directory. -r option is the one which you need to use. The following example will look for the string “ramesh” in all the files in the current directory and all it’s subdirectory.
$ grep -r "ramesh" *
9. Invert match using grep -v
You had different options to show the lines matched, to show the lines before match, and to show the lines after match, and to highlight match. So definitely You’d also want the option -v to do invert match.
When you want to display the lines which does not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word “go”.
When you want to display the lines which does not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word “go”.
$ grep -v "go" demo_text
4. Vim Word Navigation
You may want to do several navigation in relation to the words, such as:
WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.
Example to show the difference between WORD and word
* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.
10. display the lines which does not matches all the given pattern.
Syntax:
grep -v -e "pattern" -e "pattern"
$ cat test-file.txt
a
b
c
d
$ grep -v -e "a" -e "b" -e "c" test-file.txt
d
11. Counting the number of matches using grep -c
When you want to count that how many lines matches the given pattern/string, then use the option -c.
Syntax:
grep -c "pattern" filename
$ grep -c "go" demo_text
6
When you want do find out how many lines matches the pattern
$ grep -c this demo_file
3
When you want do find out how many lines that does not match the pattern
$ grep -v -c this demo_file
4
12. Display only the file names which matches the given pattern using grep -l
If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-case L) option.
When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure.
When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure.
$ grep -l this demo_*
demo_file
demo_file1
13. Show only the matched string
By default grep will show the line which matches the given pattern/string, but if you want the grep to show out only the matched string of the pattern then use the -o option.
It might not be that much useful when you give the string straight forward. But it becomes very useful when you give a regex pattern and trying to see what it matches as
It might not be that much useful when you give the string straight forward. But it becomes very useful when you give a regex pattern and trying to see what it matches as
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line
14. Show the position of match in the line
When you want grep to show the position where it matches the pattern in the file, use the following options as
Syntax:
grep -o -b "pattern" file
$ cat temp-file.txt
12345
12345
$ grep -o -b "3" temp-file.txt
2:3
8:3
Note: The output of the grep command above is not the position in the line, it is byte offset of the whole file.
15. Show line number while displaying the output using grep -n
To show the line number of file with the line matched. It does 1-based line numbering for each file. Use -n option to utilize this feature.
$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.
Find command with examples
# find -mindepth 3 -maxdepth 5 -name passwd
./usr/bin/passwd
./etc/pam.d/passwd
4. Executing Commands on the Files Found by the Find Command.
In the example below, the find command calculates the md5sum of all the files with the name MyCProgram.c (ignoring case). {} is replaced by the current file name.
# find -iname "MyCProgram.c" -exec md5sum {} \;
d41d8cd98f00b204e9800998ecf8427e ./mycprogram.c
d41d8cd98f00b204e9800998ecf8427e ./backup/mycprogram.c
d41d8cd98f00b204e9800998ecf8427e ./backup/MyCProgram.c
d41d8cd98f00b204e9800998ecf8427e ./MyCProgram.c
5. Inverting the match.
Shows the files or directories whose name are not MyCProgram.c .Since the maxdepth is 1, this will look only under current directory.
# find -maxdepth 1 -not -iname "MyCProgram.c"
.
./MybashProgram.sh
./create_sample_files.sh
./backup
./Program.c
6. Finding Files by its inode Number.
Every file has an unique inode number, using that we can identify that file. Create two files with similar name. i.e one file with a space at the end.
# touch "test-file-name"
# touch "test-file-name "
[Note: There is a space at the end]
# ls -1 test*
test-file-name
test-file-name
From the ls output, you cannot identify which file has the space at the end. Using option -i, you can view the inode number of the file, which will be different for these two files.
# ls -i1 test*
16187429 test-file-name
16187430 test-file-name
You can specify inode number on a find command as shown below. In this example, find command renames a file using the inode number.
# find -inum 16187430 -exec mv {} new-test-file-name \;
# ls -i1 *test*
16187430 new-test-file-name
16187429 test-file-name
You can use this technique when you want to do some operation with the files which are named poorly as shown in the example below. For example, the file with name — file?.txt has a special character in it. If you try to execute “rm file?.txt”, all the following three files will get removed. So, follow the steps below to delete only the “file?.txt” file.
# ls
file1.txt file2.txt file?.txt
Find the inode numbers of each file.
# ls -i1
804178 file1.txt
804179 file2.txt
804180 file?.txt
Use the inode number to remove the file that had special character in it as shown below.
# find -inum 804180 -exec rm {} \;
# ls
file1.txt file2.txt
[Note: The file with name "file?.txt" is now removed]
7. Find file based on the File-Permissions
Following operations are possible.
- Find files that match exact permission
- Check whether the given permission matches, irrespective of other permission bits
- Search by giving octal / symbolic representation
For this example, let us assume that the directory contains the following files. Please note that the file-permissions on these files are different.
# ls -l
total 0
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
---------- 1 root root 0 2009-02-19 20:31 no_for_all
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
Find files which has read permission to group. Use the following command to find all files that are readable by the world in your home directory, irrespective of other permissions for that file.
# find . -perm -g=r -type f -exec ls -l {} \;
-rw-r--r-- 1 root root 0 2009-02-19 20:30 ./everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 ./all_for_all
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
-rw-r----- 1 root root 0 2009-02-19 20:27 ./others_can_also_read
Find files which has read permission only to group.
# find . -perm g=r -type f -exec ls -l {} \;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
Find files which has read permission only to group [ search by octal ]
# find . -perm 040 -type f -exec ls -l {} \;
----r----- 1 root root 0 2009-02-19 20:27 ./others_can_only_read
8. Find all empty files (zero byte file) in your home directory and its subdirectory
Most files of the following command output will be lock-files and place holders created by other applications.
# find ~ -empty
List all the empty files only in your home directory.
# find . -maxdepth 1 -empty
List only the non-hidden empty files only in the current directory.
# find . -maxdepth 1 -empty -not -name ".*"
9. Finding the Top 5 Big Files
The following command will display the top 5 largest file in the current directory and its subdirectory. This may take a while to execute depending on the total number of files the command has to process.
# find . -type f -exec ls -s {} \; | sort -n -r | head -5
10. Finding the Top 5 Small Files
Technique is same as finding the bigger files, but the only difference the sort is ascending order.
# find . -type f -exec ls -s {} \; | sort -n | head -5
In the above command, most probably you will get to see only the ZERO byte files ( empty files ). So, you can use the following command to list the smaller files other than the ZERO byte files.
# find . -not -empty -type f -exec ls -s {} \; | sort -n | head -5
11. Find Files Based on file-type using option -type
Find only the socket files.
# find . -type s
Find all directories
# find . -type d
Find only the normal files
# find . -type f
Find all the hidden files
# find . -type f -name ".*"
Find all the hidden directories
# find -type d -name ".*"
12. Find files by comparing with the modification time of other file.
Show files which are modified after the specified file. The following find command displays all the files that are created/modified after ordinary_file.
# ls -lrt
total 0
-rw-r----- 1 root root 0 2009-02-19 20:27 others_can_also_read
----r----- 1 root root 0 2009-02-19 20:27 others_can_only_read
-rw------- 1 root root 0 2009-02-19 20:29 ordinary_file
-rw-r--r-- 1 root root 0 2009-02-19 20:30 everybody_read
-rwxrwxrwx 1 root root 0 2009-02-19 20:31 all_for_all
---------- 1 root root 0 2009-02-19 20:31 no_for_all
# find -newer ordinary_file
.
./everybody_read
./all_for_all
./no_for_all
13. Find Files by Size
Using the -size option you can find files by size.
Find files bigger than the given size
Find files bigger than the given size
# find ~ -size +100M
Find files smaller than the given size
# find ~ -size -100M
Find files that matches the exact given size
# find ~ -size 100M
Note: – means less than the give size, + means more than the given size, and no symbol means exact given size.
14. Create Alias for Frequent Find Operations
If you find some thing as pretty useful, then you can make it as an alias. And execute it whenever you want.
Remove the files named a.out frequently.
# alias rmao="find . -iname a.out -exec rm {} \;"
# rmao
Remove the core files generated by c program.
# alias rmc="find . -iname core -exec rm {} \;"
# rmc
15. Remove big archive files using find command
The following command removes *.zip files that are over 100M.
# find / -type f -name *.zip -size +100M -exec rm -i {} \;"
Remove all *.tar file that are over 100M using the alias rm100m (Remove 100M). Use the similar concepts and create alias like rm1g, rm2g, rm5g to remove file size greater than 1G, 2G and 5G respectively.
# alias rm100m="find / -type f -name *.tar -size +100M -exec rm -i {} \;"
# alias rm1g="find / -type f -name *.tar -size +1G -exec rm -i {} \;"
# alias rm2g="find / -type f -name *.tar -size +2G -exec rm -i {} \;"
# alias rm5g="find / -type f -name *.tar -size +5G -exec rm -i {} \;"
# rm100m
# rm1g
# rm2g
# rm5g
SSH command with examples
Let us review the following 5 basic command line usage of the ssh client.
- Identify SSH client version
- Login to remote host
- Transfer Files to/from remote host
- Debug SSH client connection
- SSH escape character usage: (Toggle SSH session, SSH session statistics etc.)
1. SSH Client Version:
Sometimes it may be necessary to identify the SSH client that you are currently running and it’s corresponding version number, which can be identified as shown below. Please note that Linux comes with OpenSSH.
$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
$ ssh -V
ssh: SSH Secure Shell 3.2.9.1 (non-commercial version) on i686-pc-linux-gnu
2. Login to remote host:
- The First time when you login to the remotehost from a localhost, it will display the host key not found message and you can give “yes” to continue. The host key of the remote host will be added under .ssh2/hostkeys directory of your home directory, as shown below.
localhost$ ssh -l jsmith remotehost.example.com
Host key not found from database.
Key fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
You can get a public key’s fingerprint by running
% ssh-keygen -F publickey.pub
on the keyfile.
Are you sure you want to continue connecting (yes/no)? yes
Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub
host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:06:50 -0700
jsmith@remotehost.example.com password:
remotehost.example.com$
- The Second time when you login to the remote host from the localhost, it will prompt only for the password as the remote host key is already added to the known hosts list of the ssh client.
localhost$ ssh -l jsmith remotehost.example.com
jsmith@remotehost.example.com password:
remotehost.example.com$
jsmith@remotehost.example.com password:
remotehost.example.com$
- For some reason, if the host key of the remote host is changed after you logged in for the first time, you may get a warning message as shown below. This could be because of various reasons such as 1) Sysadmin upgraded/reinstalled the SSH server on the remote host 2) someone is doing malicious activity etc., The best possible action to take before saying “yes” to the message below, is to call your sysadmin and identify why you got the host key changed message and verify whether it is the correct host key or not.
localhost$ ssh -l jsmith remotehost.example.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the host key has just been changed.
Please contact your system administrator.
Add correct host key to "/home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub"
to get rid of this message.
Received server key's fingerprint:
xabie-dezbc-manud-bartd-satsy-limit-nexiu-jambl-title-jarde-tuxum
You can get a public key's fingerprint by running % ssh-keygen -F publickey.pub on the keyfile. Agent forwarding is disabled to avoid attacks by corrupted servers. Are you sure you want to continue connecting (yes/no)? yes Do you want to change the host key on disk (yes/no)? yes Agent forwarding re-enabled. Host key saved to /home/jsmith/.ssh2/hostkeys/key_22_remotehost.example.com.pub host key for remotehost.example.com, accepted by jsmith Mon May 26 2008 16:17:31 -0700 jsmith @remotehost.example.com's password: remotehost$
3. File transfer to/from remote host:
Another common use of ssh client is to copy files from/to remote host using scp.
- Copy file from the remotehost to the localhost:
localhost$scp jsmith@remotehost.example.com:/home/jsmith/remotehostfile.txt remotehostfile.txt
- Copy file from the localhost to the remotehost:
localhost$scp localhostfile.txt jsmith@remotehost.example.com:/home/jsmith/localhostfile.txt
4. Debug SSH Client:
Sometimes it is necessary to view debug messages to troubleshoot any SSH connection issues. For this purpose, pass -v (lowercase v) option to the ssh as shown below.
- Example without debug message:
localhost$ ssh -l jsmith remotehost.example.com
warning: Connecting to remotehost.example.com failed: No address associated to the name
localhost$
warning: Connecting to remotehost.example.com failed: No address associated to the name
localhost$
- Example with debug message:
locaclhost$ ssh -v -l jsmith remotehost.example.com
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext: Metaconfig parsing stopped at line 3.
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose: Setting variable 'VerboseMode' to 'FALSE'.
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for '/home/jsmith/.ssh2/ssh2_config')
debug: Connecting to remotehost.example.com, port 22... (SOCKS not used)
warning: Connecting to remotehost.example.com failed: No address associated to the name
debug: SshConfig/sshconfig.c:2838/ssh2_parse_config_ext: Metaconfig parsing stopped at line 3.
debug: SshConfig/sshconfig.c:637/ssh_config_set_param_verbose: Setting variable 'VerboseMode' to 'FALSE'.
debug: SshConfig/sshconfig.c:3130/ssh_config_read_file_ext: Read 17 params from config file.
debug: Ssh2/ssh2.c:1707/main: User config file not found, using defaults. (Looked for '/home/jsmith/.ssh2/ssh2_config')
debug: Connecting to remotehost.example.com, port 22... (SOCKS not used)
warning: Connecting to remotehost.example.com failed: No address associated to the name
5. Escape Character: (Toggle SSH session, SSH session statistics etc.)
Escape character ~ get’s SSH clients attention and the character following the ~ determines the escape command.
Toggle SSH Session: When you’ve logged on to the remotehost using ssh from the localhost, you may want to come back to the localhost to perform some activity and go back to remote host again. In this case, you don’t need to disconnect the ssh session to the remote host. Instead follow the steps below.
Toggle SSH Session: When you’ve logged on to the remotehost using ssh from the localhost, you may want to come back to the localhost to perform some activity and go back to remote host again. In this case, you don’t need to disconnect the ssh session to the remote host. Instead follow the steps below.
- Login to remotehost from localhost: localhost$ssh -l jsmith remotehost
- Now you are connected to the remotehost: remotehost$
- To come back to the localhost temporarily, type the escape character ~ and Control-Z. When you type ~ you will not see that immediately on the screen until you press <Control-Z> and press enter. So, on the remotehost in a new line enter the following key strokes for the below to work: ~<Control-Z>
remotehost$ ~^Z
[1]+ Stopped ssh -l jsmith remotehost
localhost$
[1]+ Stopped ssh -l jsmith remotehost
localhost$
- Now you are back to the localhost and the ssh remotehost client session runs as a typical unix background job, which you can check as shown below:
localhost$ jobs
[1]+ Stopped ssh -l jsmith remotehost
[1]+ Stopped ssh -l jsmith remotehost
- You can go back to the remote host ssh without entering the password again by bringing the background ssh remotehost session job to foreground on the localhost
localhost$ fg %1
ssh -l jsmith remotehost
remotehost$
ssh -l jsmith remotehost
remotehost$
SSH Session statistics: To get some useful statistics about the current ssh session, do the following. This works only on SSH2 client.
- Login to remotehost from localhost: localhost$ssh -l jsmith remotehost
- On the remotehost, type ssh escape character ~ followed by s as shown below. This will display lot of useful statistics about the current SSH connection.
remotehost$ [Note: The ~s is not visible on the command line when you type.]
remote host: remotehost
local host: localhost
remote version: SSH-1.99-OpenSSH_3.9p1
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
compressed bytes in: 1506
uncompressed bytes in: 1622
compressed bytes out: 4997
uncompressed bytes out: 5118
packets in: 15
packets out: 24
rekeys: 0
Algorithms:
Chosen key exchange algorithm: diffie-hellman-group1-sha1
Chosen host key algorithm: ssh-dss
Common host key algorithms: ssh-dss,ssh-rsa
Algorithms client to server:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
Algorithms server to client:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
localhost$
remote host: remotehost
local host: localhost
remote version: SSH-1.99-OpenSSH_3.9p1
local version: SSH-2.0-3.2.9.1 SSH Secure Shell (non-commercial)
compressed bytes in: 1506
uncompressed bytes in: 1622
compressed bytes out: 4997
uncompressed bytes out: 5118
packets in: 15
packets out: 24
rekeys: 0
Algorithms:
Chosen key exchange algorithm: diffie-hellman-group1-sha1
Chosen host key algorithm: ssh-dss
Common host key algorithms: ssh-dss,ssh-rsa
Algorithms client to server:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
Algorithms server to client:
Cipher: aes128-cbc
MAC: hmac-sha1
Compression: zlib
localhost$
SED command with examples
I. Sed Substitution Delimiter
As we discussed in our previous post, we can use the different delimiters such as @ % | ; : in sed substitute command.
Let us first create path.txt file that will be used in all the examples mentioned below.
$ cat path.txt
/usr/kbos/bin:/usr/local/bin:/usr/jbin:/usr/bin:/usr/sas/bin
/usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin:
/opt/omni/lbin:/opt/omni/sbin:/root/bin
Example 1 – sed @ delimiter: Substitute /opt/omni/lbin to /opt/tools/bin
When you substitute a path name which has ‘/’, you can use @ as a delimiter instead of ‘/’. In the sed example below, in the last line of the input file, /opt/omni/lbin was changed to /opt/tools/bin.
$ sed 's@/opt/omni/lbin@/opt/tools/bin@g' path.txt
/usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin:/usr/sas/bin
/usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin:
/opt/tools/bin:/opt/omni/sbin:/root/bin
II. Sed ‘&’ Get Matched String
The precise part of an input line on which the Regular Expression matches is represented by &, which can then be used in the replacement part.
Example 1 – sed & Usage: Substitute /usr/bin/ to /usr/bin/local
$ sed 's@/usr/bin@&/local@g' path.txt
/usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin/local:/usr/sas/bin
/usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin/local:/opt/omni/bin:
/opt/omni/lbin:/opt/omni/sbin:/root/bin
In the above example ‘&’ in the replacement part will replace with /usr/bin which is matched pattern and add it with /local. So in the output all the occurrance of /usr/bin will be replaced with /usr/bin/local
Example 2 – sed & Usage: Match the whole line
& replaces whatever matches with the given REGEXP.
$ sed 's@^.*$@<<<&>>>@g' path.txt
<<</usr/kbos/bin:/usr/local/bin:/usr/jbin/:/usr/bin:/usr/sas/bin>>>
<<</usr/local/sbin:/sbin:/bin/:/usr/sbin:/usr/bin:/opt/omni/bin:>>>
<<</opt/omni/lbin:/opt/omni/sbin:/root/bin>>>
In the above example regexp has “^.*$” which matches the whole line. Replacement part <<<&>>> writes the whole line with <<< and >>> in the beginning and end of the line respectively.
III. Grouping and Back-references in Sed
Grouping can be used in sed like normal regular expression. A group is opened with “\(” and closed with “\)”.Grouping can be used in combination with back-referencing.
Back-reference is the re-use of a part of a Regular Expression selected by grouping. Back-references in sed can be used in both a Regular Expression and in the replacement part of the substitute command.
Example 1: Get only the first path in each line
$ sed 's/\(\/[^:]*\).*/\1/g' path.txt
/usr/kbos/bin
/usr/local/sbin
/opt/omni/lbin
In the above example, \(\/[^:]*\) matches the path available before first : comes. \1 replaces the first matched group.
Example 2: Multigrouping
In the file path.txt change the order of field in the last line of the file.
$ sed '$s@\([^:]*\):\([^:]*\):\([^:]*\)@\3:\2:\1@g' path.txt
/usr/kbos/bin:/usr/local/bin:/usr/jbin:/usr/bin:/usr/sas/bin
/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/omni/bin:
/root/bin:/opt/omni/sbin:/opt/omni/lbin
In the above command $ specifies substitution to happen only for the last line.Output shows that the order of the path values in the last line has been reversed.
Example 3: Get the list of usernames in /etc/passwd file
This sed example displays only the first field from the /etc/passwd file.
$sed 's/\([^:]*\).*/\1/' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
Example 4: Parenthesize first character of each word
This sed example prints the first character of every word in paranthesis.
$ echo "Welcome To The Geek Stuff" | sed 's/\(\b[A-Z]\)/\(\1\)/g'
(W)elcome (T)o (T)he (G)eek (S)tuff
Example 5: Commify the simple number.
Let us create file called numbers which has list of numbers. The below sed command example is used to commify the numbers till thousands.
$ cat numbers
1234
12121
3434
123
$sed 's/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g' numbers
1,234
12,121
3,434
123
Awk command with examples
Awk reads and parses each line from input based on whitespace character by default and set the variables $1,$2 and etc. Awk FS variable is used to set the field separator for each record. Awk FS can be set to any single character or regular expression. You can use input field separator using one of the following two options:
- Using -F command line option.
- Awk FS can be set like normal variable.
Syntax:
$ awk -F 'FS' 'commands' inputfilename
(or)
$ awk 'BEGIN{FS="FS";}'
- Awk FS is any single character or regular expression which you want to use as a input field separator.
- Awk FS can be changed any number of times, it retains its values until it is explicitly changed. If you want to change the field separator, its better to change before you read the line. So that change affects the line what you read.
Here is an awk FS example to read the /etc/passwd file which has “:” as field delimiter.
$ cat etc_passwd.awk
BEGIN{
FS=":";
print "Name\tUserID\tGroupID\tHomeDirectory";
}
{
print $1"\t"$3"\t"$4"\t"$6;
}
END {
print NR,"Records Processed";
}
$awk -f etc_passwd.awk /etc/passwd
Name UserID GroupID HomeDirectory
gnats 41 41 /var/lib/gnats
libuuid 100 101 /var/lib/libuuid
syslog 101 102 /home/syslog
hplip 103 7 /var/run/hplip
avahi 105 111 /var/run/avahi-daemon
saned 110 116 /home/saned
pulse 111 117 /var/run/pulse
gdm 112 119 /var/lib/gdm
8 Records Processed
2. Awk OFS Example: Output Field Separator Variable
Awk OFS is an output equivalent of awk FS variable. By default awk OFS is a single space character. Following is an awk OFS example.
$ awk -F':' '{print $3,$4;}' /etc/passwd
41 41
100 101
101 102
103 7
105 111
110 116
111 117
112 119
Concatenator in the print statement “,” concatenates two parameters with a space which is the value of awk OFS by default. So, Awk OFS value will be inserted between fields in the output as shown below.
$ awk -F':' 'BEGIN{OFS="=";} {print $3,$4;}' /etc/passwd
41=41
100=101
101=102
103=7
105=111
110=116
111=117
112=119
3. Awk RS Example: Record Separator variable
Awk RS defines a line. Awk reads line by line by default
Let us take students marks are stored in a file, each records are separated by double new line, and each fields are separated by a new line character.
$cat student.txt
Jones
2143
78
84
77
Gondrol
2321
56
58
45
RinRao
2122
38
37
65
Edwin
2537
78
67
45
Dayan
2415
30
47
20
Now the below Awk script prints the Student name and Rollno from the above input file.
$cat student.awk
BEGIN {
RS="\n\n";
FS="\n";
}
{
print $1,$2;
}
$ awk -f student.awk student.txt
Jones 2143
Gondrol 2321
RinRao 2122
Edwin 2537
Dayan 2415
In the script student.awk, it reads each student detail as a single record,because awk RS has been assigned to double new line character and each line in a record is a field, since FS is newline character.
4. Awk ORS Example: Output Record Separator Variable
Awk ORS is an Output equivalent of RS. Each record in the output will be printed with this delimiter. Following is an awk ORS example:
$ awk 'BEGIN{ORS="=";} {print;}' student-marks
Jones 2143 78 84 77=Gondrol 2321 56 58 45=RinRao 2122 38 37 65=Edwin 2537 78 67 45=Dayan 2415 30 47 20=
In the above script,each records in the file student-marks file is delimited by the character “=”.
5. Awk NR Example: Number of Records Variable
Awk NR gives you the total number of records being processed or line number. In the following awk NR example, NR variable has line number, in the END section awk NR tells you the total number of records in a file.
$ awk '{print "Processing Record - ",NR;}END {print NR, "Students Records are processed";}' student-marks
Processing Record - 1
Processing Record - 2
Processing Record - 3
Processing Record - 4
Processing Record - 5
5 Students Records are processed
6. Awk NF Example: Number of Fields in a record
Awk NF gives you the total number of fields in a record. Awk NF will be very useful for validating whether all the fields are exist in a record.
Let us take in the student-marks file, Test3 score is missing for to students as shown below.
$cat student-marks
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122 38 37
Edwin 2537 78 67 45
Dayan 2415 30 47
The following Awk script, prints Record(line) number, and number of fields in that record. So It will be very simple to find out that Test3 score is missing.
$ awk '{print NR,"->",NF}' student-marks
1 -> 5
2 -> 5
3 -> 4
4 -> 5
5 -> 4
7. Awk FILENAME Example: Name of the current input file
FILENAME variable gives the name of the file being read. Awk can accept number of input files to process.
$ awk '{print FILENAME}' student-marks
student-marks
student-marks
student-marks
student-marks
student-marks
In the above example, it prints the FILENAME i.e student-marks for each record of the input file.
8. Awk FNR Example: Number of Records relative to the current input file
When awk reads from the multiple input file, awk NR variable will give the total number of records relative to all the input file. Awk FNR will give you number of records for each input file.
$ awk '{print FILENAME, FNR;}' student-marks bookdetails
student-marks 1
student-marks 2
student-marks 3
student-marks 4
student-marks 5
bookdetails 1
bookdetails 2
bookdetails 3
bookdetails 4
bookdetails 5
In the above example, instead of awk FNR, if you use awk NR, for the file bookdetails the you will get from 6 to 10 for each record.
Vi and Vim Macro: How To Record and Play
Using Vim Macro feature you can record and play a sequence of actions inside the editor.
This article explains how to perform record and play inside Vi and Vim editor using two detailed examples.
High Level Steps to Record and Play inside Vim
- Start recording by pressing q, followed by a lower case character to name the macro
- Perform any typical editing, actions inside Vim editor, which will be recorded
- Stop recording by pressing q
- Play the recorded macro by pressing @ followed by the macro name
- To repeat macros multiple times, press : NN @ macro name. NN is a number
Example 1: Sequence number generation inside a file using Vim Macro
1. Start a sequence-test.txt file to generate the sequence.
$ vim sequence-test.txt
2. Go to insert mode and type “1 .” as shown below
Type: Esc i followed by 1.
$ vim sequence-test.txt
1.
3. Start the Recording and store it in register a.
Type: Esc q followed by a
- q indicates to start the recording
- a indicates to store the recordings in register a
- When you do q a, it will display “recording” at the bottom of the vi.
4. Copy the 1st line to 2nd line to have two lines with 1 . as shown below
Type: Esc yy followed by p
- yy will copy the current line
- p will paste the line that was just copied
$ vim sequence-test.txt
1.
1.
Note: Vim will still show recording at the bottom as shown below.

Fig: Vim showing recording message at the bottom
5. Increment the number.
Type: Control a
By placing the cursor at the 2nd line, press Ctrl+a which increment the number to 2. as shown below.
$ vim sequence-test.txt
1.
2.
Note: vim will still show recording at the bottom
6. Stop the recording
Type: q
Press q to stop the recording. You’ll notice that recording message at the bottom of the vim is now gone.
7. Repeat the recording 98 times.
Type: 98@a
- Now repeat this job, by typing 98 @ a
- @a repeats the macro “a” one time.
- 98@a repeats the macros “a” 98 times generating the sequence number 1 – 100 as shown below using macros.

Fig: Generate Sequence Number in Vim using Macro
Example 2: Repeat Vim Macro with different arguments
This example explains how you can executing the same command, with different input for it. i.e Framing the same command, with different arguments.
Before Executing the Macro: change-password.sql
$ vim change-password.sql
Annette
Warren
Anthony
Preston
Kelly
Taylor
Stiller
Dennis
Schwartz
After Recording and executing the Macro: change-password.sql
$ vim change-password.sql
ALTER USER Annette IDENTIFIED BY 'Annette';
ALTER USER Warren IDENTIFIED BY 'Warren';
ALTER USER Anthony IDENTIFIED BY 'Anthony ';
ALTER USER Preston IDENTIFIED BY 'Preston';
ALTER USER Kelly IDENTIFIED BY 'Kelly ';
ALTER USER Taylor IDENTIFIED BY 'Taylor';
ALTER USER Stiller IDENTIFIED BY 'Stiller';
ALTER USER Dennis IDENTIFIED BY 'Dennis';
ALTER USER Schwart IDENTIFIED BY 'Schwart';
1. Open the change-password.sql that has only the names.
$ vim change-password.sql
Annette
Warren
Anthony
Preston
Kelly
Taylor
Stiller
Dennis
Schwartz
2. Start the Recording and store it in register a
Type: q a
- q indicates to start the recording
- a indicates to store the recordings in register a
- When you do q a, it will display the message recording at the bottom of the vi.
3. Go to Insert Mode and Type ALTER USER
Type: I (Upper case i) followed by “ALTER USER ”
Place the cursor anywhere in the first line, and then press I. Which will take you to the first character of the line. Type ALTER USER
4. Copy the Next Word (i.e the name)
Type: Esc w yw
- Press Esc, and then press w to go to the next word ( name ).
- yw, copies the current word ( name ).
5.Go to the end and type IDENTIFIED BY ‘
Type: Esc A followed by ” IDENTIFIED BY ‘”
- Press Esc, and A to move the cursor to the end of the line, and then type space.
- Type IDENTIFIED BY ‘
6. Paste the copied Name
Type: Esc p
Press Esc, and then type p to paste the name that was copied in the step #4.
7. Complete the quote at the end.
Type: Esc A followed by ‘;
Press Esc, and A to go to the end of the line, and ‘;
8. Jump to the next line and stop the record.
Type: Esc j followed by q
- j to move to the next line.
- q to stop the recording
Note: The recording message shown in the bottom of the vi will now disappear. At this stage, the change-password.sql will look like the following.

Fig: Vim Macro completed the recording
9. Repete the Macro with the arguments in the corresponding line
Type: 8 @ a
- Now repeat this job 8 times by typing 8@a
- @a repeats the macro “a” one time.
- 8@a repeats the macros “a” 8 times completing the rest of the line automatically as shown below

Fig: Vim Macro Play completed
Diff command with examples
For finding difference between two versions of a file on Linux, you can use any one of the 4 tools explained in this article — diff, colordiff, wdiff, and vimdiff.
The screenshots provided for these tools shows the difference between the following two empfile1.txt and empfile2.txt.
$ cat empfile1.txt
John Smith 1001 Sr. Engineer
Peter 1002 Engineer
Fernandous 1003 Sr. Engineer
Kraml 1004 Jr. Engineer
$ cat empfile2.txt
John Smith 1001 Sr. Engineer
Peter 1002 Engineer
Fernandous 1003 Resigned
Kraml 1004 Jr. Engineer
Raj 1005 Engineer
1. Diff Command
This is the traditional way to find out the difference two files is using diff command.
$ diff empfile1.txt empfile2.txt
3c3
< Fernandous 1003 Sr. Engineer
---
> Fernandous 1003 Resigned
5c5
<
---
> Raj 1005 Engineer
sort command examples
Sort a file in ascending order$ sort names.txtSort a file in descending order$ sort -r names.txtSort passwd file by 3rd field.$ sort -t: -k 3n /etc/passwd | moreexport command examples
To view oracle related environment variables.$ export | grep ORACLEdeclare -x ORACLE_BASE="/u01/app/oracle"declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"declare -x ORACLE_SID="med"declare -x ORACLE_TERM="xterm"To export an environment variable:$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0
xargs command examples
Copy all images to external hard-drive# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directorySearch all jpg images in the system and archive it.# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gzDownload all the URLs mentioned in the url-list.txt file# cat url-list.txt | xargs wget –cls – Unix users and sysadmins cannot live without this two letter command. Whether you use it 10 times a day or 100 times a day, knowing the power of ls command can make your command line journey enjoyable. In this article, let us review 15 practical examples of the mighty ls command.Ls command with examples1. Open Last Edited File Using ls -t
To open the last edited file in the current directory use the combination of ls, head and vi commands as shown below. ls -t sorts the file by modification time, showing the last edited file first. head -1 picks up this first file.$ vi first-long-file.txt$ vi second-long-file.txt$ vi `ls -t | head -1`
[Note: This will open the last file you edited (i.e second-long-file.txt)]
2. Display One File Per Line Using ls -1
To show single entry per line, use -1 option as shown below.$ ls -1binbootcdromdevetchomeinitrdinitrd.imglib3. Display All Information About Files/Directories Using ls -l
To show long listing information about the file/directory.$ ls -l-rw-r----- 1 ramesh team-dev 9275204 Jun 13 15:27 mthesaur.txt.gz
- 1st Character – File Type: First character specifies the type of the file. In the example above the hyphen (-) in the 1st character indicates that this is a normal file. Following are the possible file type options in the 1st character of the ls -l output.
- Field Explanation
- - normal file
- d directory
- s socket file
- l link file
- Field 1 – File Permissions: Next 9 character specifies the files permission. Each 3 characters refers to the read, write, execute permissions for user, group and world In this example, -rw-r—– indicates read-write permission for user, read permission for group, and no permission for others.
- Field 2 – Number of links: Second field specifies the number of links for that file. In this example, 1 indicates only one link to this file.
- Field 3 – Owner: Third field specifies owner of the file. In this example, this file is owned by username ‘ramesh’.
- Field 4 – Group: Fourth field specifies the group of the file. In this example, this file belongs to ”team-dev’ group.
- Field 5 – Size: Fifth field specifies the size of file. In this example, ’9275204′ indicates the file size.
- Field 6 – Last modified date & time: Sixth field specifies the date and time of the last modification of the file. In this example, ‘Jun 13 15:27′ specifies the last modification time of the file.
- Field 7 – File name: The last field is the name of the file. In this example, the file name is mthesaur.txt.gz.
4.
Display File Size in Human Readable Format Using ls -lh
Use ls -lh (h stands for
human readable form), to display file size in easy to read format. i.e M for
MB, K for KB, G for GB.
$
ls -l
-rw-r-----
1 ramesh team-dev 9275204 Jun 12 15:27 arch-linux.txt.gz*
$
ls -lh
-rw-r-----
1 ramesh team-dev 8.9M Jun 12 15:27
arch-linux.txt.gz
5.
Display Directory Information Using ls -ld
When you use “ls -l” you will get
the details of directories content. But if you want the details of directory
then you can use -d option as., For example, if you use ls -l /etc will display
all the files under etc directory. But, if you want to display the information
about the /etc/ directory, use -ld option as shown below.
$
ls -l /etc
total
3344
-rw-r--r-- 1 root root
15276 Oct 5 2004 a2ps.cfg
-rw-r--r-- 1 root root 2562 Oct
5 2004 a2ps-site.cfg
drwxr-xr-x 4 root root 4096 Feb
2 2007 acpi
-rw-r--r-- 1 root root 48 Feb
8 2008 adjtime
drwxr-xr-x 4 root root 4096 Feb
2 2007 alchemist
$
ls -ld /etc
drwxr-xr-x
21 root root 4096 Jun 15 07:02 /etc
6.
Order Files Based on Last Modified Time Using ls -lt
To sort the file names displayed in
the order of last modification time use the -t option. You will be finding it
handy to use it in combination with -l option.
$
ls -lt
total
76
drwxrwxrwt 14 root root
4096 Jun 22 07:36 tmp
drwxr-xr-x
121 root root 4096 Jun 22 07:05 etc
drwxr-xr-x 13 root root 13780 Jun 22 07:04 dev
drwxr-xr-x 13 root root
4096 Jun 20 23:12 root
drwxr-xr-x 12 root root
4096 Jun 18 08:31 home
drwxr-xr-x 2 root root
4096 May 17 21:21 sbin
lrwxrwxrwx 1 root root 11 May 17 20:29 cdrom -> media/cdrom
drwx------ 2 root root 16384 May 17 20:29 lost+found
drwxr-xr-x 15 root root
4096 Jul 2 2008 var
7.
Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
To sort the file names in the last
modification time in reverse order. This will be showing the last edited file
in the last line which will be handy when the listing goes beyond a page. This
is my default ls usage. Anytime I do ls, I always use ls -ltr as I find this
very convenient.
$
ls -ltr
total
76
drwxr-xr-x 15 root root
4096 Jul 2 2008 var
drwx------ 2 root root 16384 May 17 20:29 lost+found
lrwxrwxrwx 1 root root 11 May 17 20:29 cdrom -> media/cdrom
drwxr-xr-x 2 root root
4096 May 17 21:21 sbin
drwxr-xr-x 12 root root
4096 Jun 18 08:31 home
drwxr-xr-x 13 root root
4096 Jun 20 23:12 root
drwxr-xr-x 13 root root 13780 Jun 22 07:04 dev
drwxr-xr-x
121 root root 4096 Jun 22 07:05 etc
drwxrwxrwt 14 root root
4096 Jun 22 07:36 tmp
8.
Display Hidden Files Using ls -a (or) ls -A
To show all the hidden files in the
directory, use ‘-a option’. Hidden files in Unix starts with ‘.’ in its file
name.
$
ls -a
[rnatarajan@asp-dev
~]$ ls -a
. Debian-Info.txt
.. CentOS-Info.txt
.bash_history Fedora-Info.txt
.bash_logout .lftp
.bash_profile libiconv-1.11.tar.tar
.bashrc
libssh2-0.12-1.2.el4.rf.i386.rpm
It will show all the files including
the ‘.’ (current directory) and ‘..’ (parent directory). To show the hidden
files, but not the ‘.’ (current directory) and ‘..’ (parent directory), use
option -A.
$
ls -A
Debian-Info.txt Fedora-Info.txt
CentOS-Info.txt Red-Hat-Info.txt
.bash_history SUSE-Info.txt
.bash_logout .lftp
.bash_profile libiconv-1.11.tar.tar
.bashrc
libssh2-0.12-1.2.el4.rf.i386.rpm
[Note:
. and .. are not displayed here]
9.
Display Files Recursively Using ls -R
$
ls /etc/sysconfig/networking
devices profiles
$
ls -R /etc/sysconfig/networking
/etc/sysconfig/networking:
devices profiles
/etc/sysconfig/networking/devices:
/etc/sysconfig/networking/profiles:
default
/etc/sysconfig/networking/profiles/default:
To show all the files recursively,
use -R option. When you do this from /, it shows all the unhidden files in the
whole file system recursively.
10.
Display File Inode Number Using ls -i
Sometimes you may want to know the
inone number of a file for internal maintenance. Use -i option as shown below
to display inone number. Using inode number you can remove files that has
special characters in it’s name as explained in the example#6 of the find command article.
$
ls -i /etc/xinetd.d/
279694
chargen 279724 cups-lpd 279697 daytime-udp
279695
chargen-udp 279696 daytime 279698 echo
11.
Hide Control Characters Using ls -q
To print question mark instead of
the non graphics control characters use the -q option.
ls
-q
12.
Display File UID and GID Using ls -n
Lists the output like -l, but shows
the uid and gid in numeric format instead of names.
$
ls -l ~/.bash_profile
-rw-r--r-- 1 ramesh ramesh 909 Feb 8 11:48 /home/ramesh/.bash_profile
$
ls -n ~/.bash_profile
-rw-r--r-- 1 511 511 909 Feb 8 11:48 /home/ramesh/.bash_profile
[Note:
This display 511 for uid and 511 for gid]
13.
Visual Classification of Files With Special Characters Using ls -F
Instead of doing the ‘ls -l’ and
then the checking for the first character to determine the type of file. You
can use -F which classifies the file with different special character for
different kind of files.
$
ls -F
Desktop/ Documents/
Ubuntu-App@ firstfile Music/
Public/ Templates/
Thus in the above output,
- / – directory.
- nothing – normal file.
- @ – link file.
- * – Executable file
14.
Visual Classification of Files With Colors Using
ls -F
Recognizing the file type by the
color in which it gets displayed is an another kind in classification of file.
In the above output directories get displayed in blue, soft links get displayed
in green, and ordinary files gets displayed in default color.
$
ls --color=auto
Desktop Documents
Examples firstfile Music Pictures
Public Templates Videos
15.
Useful ls Command Aliases
You can take some required ls
options in the above, and make it as aliases. We suggest the following.
- Long list the file with size in human understandable form.
alias
ll="ls -lh"
- Classify the file type by appending special characters.
alias
lv="ls -F"
- Classify the file type by both color and special character.
alias
ls="ls -F --color=auto"
pwd command
pwd is Print working directory. What
else can be said about the good old pwd who has been printing the current
directory name for ages.
Use
CDPATH to define the base directory for cd command
If you are frequently doing cd to
subdirectories of a specific parent directory, you can set the CDPATH to the
parent directory and perform cd to the subdirectories without giving the parent
directory path as explained below.
[ramesh@dev-db
~]# pwd
/home/ramesh
[ramesh@dev-db
~]# cd mail
-bash:
cd: mail: No such file or directory
[Note: This is looking for mail directory under current directory]
[ramesh@dev-db
~]# export CDPATH=/etc
[ramesh@dev-db
~]# cd mail
[Note: This is looking for mail under /etc and not under current
directory]
[ramesh@dev-db
/etc/mail]# pwd
/etc/mail
To make this change permanent, add export
CDPATH=/etc to your ~/.bash_profile
This hack can be very helpful under
the following situations:
- Oracle DBAs frequently working under $ORACLE_HOME, can set the CDPATH variable to the oracle home
- Unix sysadmins frequently working under /etc, can set the CDPATH variable to /etc
- Developers frequently working under project directory /home/projects, can set the CDPATH variable to /home/projects
- End-users frequently accessing the subdirectories under their home directory, can set the CDPATH variable to ~ (home directory)
Use
cd alias to navigate up the directory effectively
When you are navigating up a very
long directory structure, you may be using cd ..\..\ with multiple ..\’s
depending on how many directories you want to go up as shown below.
#
mkdir -p /tmp/very/long/directory/structure/that/is/too/deep
#
cd /tmp/very/long/directory/structure/that/is/too/deep
#
pwd
/tmp/very/long/directory/structure/that/is/too/deep
#
cd ../../../../
#
pwd
/tmp/very/long/directory/structure
Instead of executing cd ../../../..
to navigate four levels up, use one of the following alias methods:
Navigate up the directory using ..n : In the example below, ..4 is used
to go up 4 directory level, ..3 to go up 3 directory level, ..2 to go up 2
directory level. Add the following alias to the .bash_profile and re-login.
alias
..="cd .."
alias
..2="cd ../.."
alias
..3="cd ../../.."
alias
..4="cd ../../../.."
alias
..5="cd ../../../../.."
#
cd /tmp/very/long/directory/structure/that/is/too/deep
#..4
[Note: use ..4 to go up 4 directory level]
#
pwd
/tmp/very/long/directory/structure/
Navigate up the directory using only dots: In the example below, …..
(five dots) is used to go up 4 directory level. Typing 5 dots to go up 4
directory structure is really easy to remember, as when you type the first two
dots, you are thinking “going up one directory”, after that every additional
dot, is to go one level up. So, use …. (four dots) to go up 3 directory level
and .. (two dots) to go up 1 directory level. Add the following alias to the
.bash_profile and re-login for the ….. (five dots) to work properly.
alias
..="cd .."
alias
...="cd ../.."
alias
....="cd ../../.."
alias
.....="cd ../../../.."
alias
......="cd ../../../../.."
#
cd /tmp/very/long/directory/structure/that/is/too/deep
#
.....
[Note: use ..... (five dots) to go up 4 directory level]
#
pwd
/tmp/very/long/directory/structure/
Navigate up the directory using cd followed by consecutive dots: In the
example below, cd….. (cd followed by five dots) is used to go up 4 directory
level. Making it 5 dots to go up 4 directory structure is really easy to
remember, as when you type the first two dots, you are thinking “going up one
directory”, after that every additional dot, is to go one level up. So, use
cd…. (cd followed by four dots) to go up 3 directory level and cd… (cd followed
by three dots) to go up 2 directory level. Add the following alias to the
.bash_profile and re-login for the above cd….. (five dots) to work properly.
alias
cd..="cd .."
alias
cd...="cd ../.."
alias
cd....="cd ../../.."
alias
cd.....="cd ../../../.."
alias
cd......="cd ../../../../.."
#
cd /tmp/very/long/directory/structure/that/is/too/deep
#
cd.....
[Note: use cd..... to go up 4 directory level]
#
pwd
/tmp/very/long/directory/structure
Perform
mkdir and cd using a single command
Sometimes when you create a new
directory, you may cd to the new directory immediately to perform some work as
shown below.
#
mkdir -p /tmp/subdir1/subdir2/subdir3
#
cd /tmp/subdir1/subdir2/subdir3
#
pwd
/tmp/subdir1/subdir2/subdir3
Wouldn’t it be nice to combine both
mkdir and cd in a single command? Add the following to the .bash_profile and
re-login.
function
mkdircd () { mkdir -p "$@" && eval cd
"\"\$$#\""; }
Now, perform both mkdir and cd at
the same time using a single command as shown below:
#
mkdircd /tmp/subdir1/subdir2/subdir3
[Note: This creates the directory and cd to it automatically]
#
pwd
/tmp/subdir1/subdir2/subdir3
Use
“cd -” to toggle between the last two directories
You can toggle between the last two
current directories using cd – as shown below.
#
cd /tmp/very/long/directory/structure/that/is/too/deep
#
cd /tmp/subdir1/subdir2/subdir3
#
cd -
#
pwd
/tmp/very/long/directory/structure/that/is/too/deep
#
cd -
#
pwd
/tmp/subdir1/subdir2/subdir3
#
cd -
#
pwd
/tmp/very/long/directory/structure/that/is/too/deep
Note: You can also substitute an argument from other commands in
the history to the cd command using example#12 and #13 mentioned in the command line history
examples article.
Use
dirs, pushd and popd to manipulate directory stack
You can use directory stack to push
directories into it and later pop directory from the stack. Following three
commands are used in this example.
- dirs: Display the directory stack
- pushd: Push directory into the stack
- popd: Pop directory from the stack and cd to it
Dirs will always print the current
directory followed by the content of the stack. Even when the directory stack
is empty, dirs command will still print only the current directory as shown
below.
#
popd
-bash:
popd: directory stack empty
#
dirs
~
#
pwd
/home/ramesh
How to use pushd and popd? Let us
first create some temporary directories and push them to the directory stack as
shown below.
#
mkdir /tmp/dir1
#
mkdir /tmp/dir2
#
mkdir /tmp/dir3
#
mkdir /tmp/dir4
#
cd /tmp/dir1
#
pushd .
#
cd /tmp/dir2
#
pushd .
#
cd /tmp/dir3
#
pushd .
#
cd /tmp/dir4
#
pushd .
#
dirs
/tmp/dir4
/tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1
[Note:
The first directory (/tmp/dir4) of the dir command output is always
the current directory and not the
content from the stack.]
At this stage, the directory stack
contains the following directories:
/tmp/dir4
/tmp/dir3
/tmp/dir2
/tmp/dir1
The last directory that was pushed
to the stack will be at the top. When you perform popd, it will cd to the top
directory entry in the stack and remove it from the stack. As shown above, the
last directory that was pushed into the stack is /tmp/dir4. So, when we do a
popd, it will cd to the /tmp/dir4 and remove it from the directory stack as
shown below.
#
popd
#
pwd
/tmp/dir4
[Note:
After the above popd, directory Stack Contains:
/tmp/dir3
/tmp/dir2
/tmp/dir1]
#
popd
#
pwd
/tmp/dir3
[Note:
After the above popd, directory Stack Contains:
/tmp/dir2
/tmp/dir1]
#
popd
#
pwd
/tmp/dir2
[Note:
After the above popd, directory Stack Contains: /tmp/dir1]
#
popd
#
pwd
/tmp/dir1
[Note:
After the above popd, directory Stack is empty!]
#
popd
-bash:
popd: directory stack empty
Use
“shopt -s cdspell” to automatically correct mistyped directory names on cd
Use shopt -s cdspell to
correct the typos in the cd command automatically as shown below. If you are
not good at typing and make lot of mistakes, this will be very helpful.
#
cd /etc/mall
-bash:
cd: /etc/mall: No such file or directory
#
shopt -s cdspell
#
cd /etc/mall
#
pwd
/etc/mail
[Note: By mistake, when I typed mall instead of mail,
cd corrected it
automatically]
gzip command examples
To create a *.gz compressed file:$ gzip test.txtTo uncompress a *.gz file:
$ gzip -d test.txt.gzDisplay compression ratio of the compressed file using gzip -l
$ gzip -l *.gz
compressed uncompressed ratio uncompressed_name
23709 97975 75.8% asp-patch-rpms.txt
bzip2 command examples
bzip2 command is used for compression and decompression of files. The main advantage of bzip2 is the best compression size. bzip2 vs gzip: bzip2 will compress better than gzip. Speed of bzip2 is somewhat slower than gzip and zip. bzip2 provides high rate of compression with reasonable speed. There are several Linux bz commands available to manipulate the bzip2 files. This article explains various bz commands with 6 practical examples.
Example 1: Compressing a file using bzip2
When you compress a file using bzip2 command, it creates a compressed file with *.bz2 extension as shown below.$ bzip2 trace
$ ls -l trace.bz2
-rw-r--r-- 1 root root 54167 Jan 23 2009 trace.bz2
Example 2: Search operation in bzip2 file using bzgrep
bzgrep command is used to search for a string or a pattern (regular expression) on bzip2 compressed files. bzgrep will apply grep to data from files in the bzip2 format without requiring on-disk decompression. So all the options of a grep command will be applied for bzgrep also.Syntax:
bzgrep grep-options -e pattern filenameIn the below example, trace.bz2 is a compressed trace file which is of size 58M.
$ bzgrep -i "CONSOLE=.*" trace.bz2
2010-10-11T08:40:28.100 gs(16985): CONSOLE=/dev/pts/0
2010-10-11T08:40:29.772 gs(17031): CONSOLE=/dev/pts/0
2010-10-11T08:40:58.140 gs(17099): CONSOLE=/dev/pts/0
2010-10-11T08:41:27.547 gs(17164): CONSOLE=/dev/pts/0
2010-10-11T08:41:57.962 gs(17233): CONSOLE=/dev/pts/0
2010-10-11T08:42:28.392 gs(17294): CONSOLE=/dev/pts/0
2010-10-11T08:42:57.721 gs(17439): CONSOLE=/dev/pts/0If bzgrep is not there, you have to decompress the file manually and do a grep on that, where bzgrep does this process internally and gives you the required output. bzegrep and bzfgrep commands will apply egrep and freg operation on bzip2 files respectively.
Example 3: View the bzip2 file using bzcat
If you want only to read the compressed file without decompressing it, use the bzcat command as shown below.$ bzcat trace.bz2
0: ERR: Wed Sep 22 09:59:42 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
0: ERR: Wed Sep 22 09:59:47 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
Retry. (OpC51-22)
Database: ORA-01653: unable to extend table OPC_OP.OPC_HIST_MESSAGES by 64 in tablespace OPC_6
(OpC50-15)
.
.bzcat command displays the uncompressed content into standard output file for the users to view the content.
Example 4. Paging bzip2 compressed file with bzless & bzmore
bzless and bzmore command allows you to view the content of bzip2 compressed files page by page. bzmore works on files compressed with bzip2 and also on uncompressed files.$ bzless trace.bz2
$ bzmore trace.bz2
0: ERR: Wed Sep 22 09:59:42 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
0: ERR: Wed Sep 22 09:59:47 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.OP
C_HIST_MESSAGES (OpC50-15)
Retry. (OpC51-22)
Database: ORA-01653: unable to extend table OPC_OP.OPC_HIST_MESSAGES by 64 in tablespace OPC_6
(OpC50-15)
.
.
--More--
Example 5. Compare bzip2 files using bzcmp
You can compare two bzip2 compressed file using bzcmp command. It internally uses cmp command to compare the content of the compressed contents. Here you can see the output of comparison of the two normal files and compressed files.$ cmp System.txt.001 System.txt.002
System.txt.001 System.txt.002 differ: byte 20, line 2
$ bzcmp System.txt.001.bz2 System.txt.002.bz2
- /tmp/bzdiff.csgqG32029 differ: byte 20, line 2
Example 6. Find the difference of two bzip2 files using bzdiff
In Linux, diff command will compare two files and give you the lowdown on just how different they are. If you give bz2 files to diff command, it will not be in a position to explain the difference. For bzip2 compressed files, bzdiff command gives the differences of two bzip2 compressed files as shown below.$ bzdiff System.txt.001.bz2 System.txt.002.bz2
2c2
< 0: ERR: Mon Sep 27 12:19:34 2010: gs(11153/1105824064): [chk_sqlcode.scp:92]: Database: ORA-01654: unable to extend index OPC_OP.OPCX
_ANNO_NUM by 64 in tablespace OPC_INDEX1
---
> 0: ERR: Wed Sep 22 09:59:42 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.
OPC_HIST_MESSAGES by 64 in tablespace OPC_6
4,5c4
< Retry. (OpC51-22)
< Database: ORA-01654: unable to extend index OPC_OP.OPCX_ANNO_NUM by 64 in tablespace OPC_INDEX1
---
> 0: ERR: Wed Sep 22 09:59:47 2010: gs(11153/47752677794640): [chk_sqlcode.scp:92]: Database: ORA-01653: unable to extend table OPC_OP.
OPC_HIST_MESSAGES by 64 in tablespace OPC_6
unzip command examples
To extract a *.zip compressed file:$ unzip test.zipView the contents of *.zip file (Without unzipping it):
$ unzip -l jasper.zip
Archive: jasper.zip
Length Date Time Name
-------- ---- ---- ----
40995 11-30-98 23:50 META-INF/MANIFEST.MF
32169 08-25-98 21:07 classes_
15964 08-25-98 21:07 classes_names
10542 08-25-98 21:07 classes_ncomp
-----------------------------------------------------------------------------
shutdown command examples
Shutdown the system and turn the power off immediately.# shutdown -h nowShutdown the system after 10 minutes.
# shutdown -h +10Reboot the system using shutdown command.
# shutdown -r nowForce the filesystem check during reboot.
# shutdown -Fr now
ftp command with examples
1. Connect to a FTP site
Connect to a particular FTP server using ftp command as shown below. Syntax:$ ftp IP/hostname
or
$ ftp
ftp> open IP/hostnameYou can directly open connection with a remote host using it’s IP or host name from the command line. You can also go to ftp prompt and use open command to connect with remote host. It will ask you for the user name and password to login. On some public domain FTP server, you can use “anonymous” username with any email address as the password to connect.
2. Download a file using ftp
Use the get command to download file from a remote ftp server as shown below.ftp> get FILENAMEYou have to be in the right mode to download files. i.e binary or ascii mode. Use ascii mode for transferring text files, and binary mode for all other type of files. Download the file and save it with another name. In the following example, index.html file will be downloaded and saved as my.html on the local server.
ftp>
get index.html my.html
Fetching
/home/groups/index.html to my.html
/home/groups/index.html 100% 2886 1.4KB/s
00:02
3.
Changing FTP Mode to binary or ascii
Go to ftp Ascii mode
ftp>
ascii
200
Type set to A.
Go to ftp Binary mode
ftp>
binary
200
Type set to I.
4.
Uploading a file to FTP server
Use put command to upload a file to
a remote ftp server as shown below.
ftp>
put filename
5.
Changing the remote and local directory
Apart from downloading or uploading
a file, you may want to change either the remote or local directory, which you
can do using cd and lcd respectively.
Change the remote server current
directory using cd command
ftp>
pwd
257
"/myftpserver" is current directory.
ftp>
cd dir1
250
CWD command successful. "/myftpserver/dir1" is current directory.
ftp>
pwd
257
"/myftpserver/dir1" is current directory.
Change the local machine current
directory using lcd command
ftp>
!
$
pwd
/home/sathiya/FTP
$
exit
exit
ftp>
lcd /tmp
Local
directory now /tmp
ftp>
!
$
pwd
/tmp
Note:
- executing ! takes you to the shell.
- prompt starts with ftp> is ftp prompt.
- prompt starts with $ is shell command line.
6.
Listing the contents of remote directory from FTP
You can view the content of a remote
directory using the ls / dir command.
ftp>
ls
7.
FTP Help
Type help or ? to view
list of all available ftp commands.
For a detailed help on a particular
ftp command use:
ftp>
help COMMAND
8.
Downloading multiple files with mget command
mget is for fetching multiple files
from ftp server. You can use globs to download multiple files. For example,
*.html will download all html files. The glob expansion are done on the remote
server. So, it depends on the operating system of the remote server.
ftp>
mget *.html
Fetching
/ftptest/features.html to features.html
/ftptest/features.html 100% 2256 2.2KB/s
00:01
Fetching
/ftptest/index.html to index.html
/ftptest/index.html 100% 2886 2.8KB/s
00:01
Fetching
/ftptest/othertools.html to othertools.html
/ftptest/othertools.html 100% 2282 2.2KB/s
00:01
Fetching
/ftptest/samplereport.html to samplereport.html
/ftptest/samplereport.html 100% 15KB
7.3KB/s 00:02
Fetching
/ftptest/usage.html to usage.html
/ftptest/usage.html 100% 2340 2.3KB/s
00:01
To view the file names before
downloading, you can also use mls command as shown below.
ftp>
mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html
9.
Uploading multiple files with mput command
Use mput to upload multiple files
together. This works similar to the mget command. The following example uploads
all the *.html file from local server to remote server.
ftp>
mput *.html
10.
Close a FTP connection
Without exiting the ftp prompt you
may want to open a connection to another server. In that case, execute close
command.
ftp>
open ftp.your_server.com
Already
connected to NNN.com, use close first.
ftp>
close
221
Goodbye.
ftp> open ftp.your_server.com
crontab commands with
examples
An experienced Linux sysadmin knows the importance of
running the routine maintenance jobs in the background automatically.
Linux Cron utility is an effective way to schedule a routine background job at
a specific time and/or day on an on-going basis.
Linux
Crontab Format
MIN
HOUR DOM MON DOW CMD
Table: Crontab Fields and Allowed Ranges (Linux Crontab Syntax)
| ||
Field
|
Description
|
Allowed Value
|
MIN
|
Minute field
|
0 to 59
|
HOUR
|
Hour field
|
0 to 23
|
DOM
|
Day of Month
|
1-31
|
MON
|
Month field
|
1-12
|
DOW
|
Day Of Week
|
0-6
|
CMD
|
Command
|
Any command to be executed.
|
1.
Scheduling a Job For a Specific Time
The basic usage of cron is to
execute a job in a specific time as shown below. This will execute the Full
backup shell script (full-backup) on 10th June 08:30 AM.
Please note that the time field uses 24 hours format. So, for 8 AM use 8, and
for 8 PM use 20.
30
08 10 06 * /home/ramesh/full-backup
- 30 – 30th Minute
- 08 – 08 AM
- 10 – 10th Day
- 06 – 6th Month (June)
- * – Every day of the week
2.
Schedule a Job For More Than One Instance (e.g. Twice a Day)
The following script take a
incremental backup twice a day every day.
This example executes the specified incremental backup shell script
(incremental-backup) at 11:00 and 16:00 on every day. The comma separated value
in a field specifies that the command needs to be executed in all the mentioned
time.
00
11,16 * * * /home/ramesh/bin/incremental-backup
- 00 – 0th Minute (Top of the hour)
- 11,16 – 11 AM and 4 PM
- * – Every day
- * – Every month
- * – Every day of the week
3.
Schedule a Job for Specific Range of Time (e.g. Only on Weekdays)
If you wanted a job to be scheduled
for every hour with in a specific range of time then use the following.
Cron
Job everyday during working hours
This example checks the status of
the database everyday (including weekends) during the working hours 9 a.m – 6
p.m
00
09-18 * * * /home/ramesh/bin/check-db-status
- 00 – 0th Minute (Top of the hour)
- 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
- * – Every day
- * – Every month
- * – Every day of the week
Cron
Job every weekday during working hours
This example checks the status of
the database every weekday (i.e excluding Sat and Sun) during the working hours
9 a.m – 6 p.m.
00
09-18 * * 1-5 /home/ramesh/bin/check-db-status
- 00 – 0th Minute (Top of the hour)
- 09-18 – 9 am, 10 am,11 am, 12 am, 1 pm, 2 pm, 3 pm, 4 pm, 5 pm, 6 pm
- * – Every day
- * – Every month
- 1-5 -Mon, Tue, Wed, Thu and Fri (Every Weekday)
4.
How to View Crontab Entries?
View
Current Logged-In User’s Crontab entries
To view your crontab entries type
crontab -l from your unix account as shown below.
ramesh@dev-db$
crontab -l
@yearly
/home/ramesh/annual-maintenance
*/10
* * * * /home/ramesh/check-disk-space
[Note:
This displays crontab of the current logged in user]
View
Root Crontab entries
Login as root user (su – root) and
do crontab -l as shown below.
root@dev-db#
crontab -l
no
crontab for root
Crontab
HowTo: View Other Linux User’s Crontabs entries
To view crontab entries of other
Linux users, login to root and use -u {username} -l as shown below.
root@dev-db#
crontab -u sathiya -l
@monthly
/home/sathiya/monthly-backup
00
09-18 * * * /home/sathiya/check-db-status
5.
How to Edit Crontab Entries?
Edit
Current Logged-In User’s Crontab entries
To edit a crontab entries, use
crontab -e as shown below. By default this will edit the current logged-in
users crontab.
ramesh@dev-db$
crontab -e
@yearly
/home/ramesh/centos/bin/annual-maintenance
*/10
* * * * /home/ramesh/debian/bin/check-disk-space
~
"/tmp/crontab.XXXXyjWkHw"
2L, 83C
[Note:
This will open the crontab file in Vim editor for editing.
Please
note cron created a temporary /tmp/crontab.XX... ]
When you save the above temporary
file with :wq, it will save the crontab and display the following message
indicating the crontab is successfully modified.
~
"crontab.XXXXyjWkHw"
2L, 83C written
crontab:
installing new crontab
Edit
Root Crontab entries
Login as root user (su – root) and
do crontab -e as shown below.
root@dev-db#
crontab -e
Edit
Other Linux User’s Crontab File entries
To edit crontab entries of other
Linux users, login to root and use -u {username} -e as shown below.
root@dev-db#
crontab -u sathiya -e
@monthly
/home/sathiya/fedora/bin/monthly-backup
00
09-18 * * * /home/sathiya/ubuntu/bin/check-db-status
~
~
~
"/tmp/crontab.XXXXyjWkHw"
2L, 83C
6.
Schedule a Job for Every Minute Using Cron.
Ideally you may not have a
requirement to schedule a job every minute. But understanding this example will
will help you understand the other examples mentioned below in this article.
*
* * * * CMD
The * means all the possible unit —
i.e every minute of every hour through out the year. More than using this *
directly, you will find it very useful in the following cases.
- When you specify */5 in minute field means every 5 minutes.
- When you specify 0-10/2 in minute field mean every 2 minutes in the first 10 minute.
- Thus the above convention can be used for all the other 4 fields.
7.
Schedule a Background Cron Job For Every 10 Minutes.
Use the following, if you want to
check the disk space every 10 minutes.
*/10
* * * * /home/ramesh/check-disk-space
It executes the specified command
check-disk-space every 10 minutes through out the year. But you may have a
requirement of executing the command only during office hours or vice versa.
The above examples shows how to do those things.
Instead of specifying values in the 5 fields, we can specify it using a single
keyword as mentioned below.
There are special cases in which instead of the above 5 fields you can use @
followed by a keyword — such as reboot, midnight, yearly, hourly.
Table: Cron special keywords and its meaning
| |
Keyword
|
Equivalent
|
@yearly
|
0 0 1 1 *
|
@daily
|
0 0 * * *
|
@hourly
|
0 * * * *
|
@reboot
|
Run at startup.
|
8.
Schedule a Job For First Minute of Every Year using @yearly
If you want a job to be executed on
the first minute of every year, then you can use the @yearly cron
keyword as shown below.
This will execute the system annual maintenance using annual-maintenance shell
script at 00:00 on Jan 1st for every year.
@yearly
/home/ramesh/red-hat/bin/annual-maintenance
9.
Schedule a Cron Job Beginning of Every Month using @monthly
It is as similar as the @yearly as
above. But executes the command monthly once using @monthly cron
keyword.
This will execute the shell script tape-backup at 00:00 on 1st of every month.
@monthly
/home/ramesh/suse/bin/tape-backup
10.
Schedule a Background Job Every Day using @daily
Using the @daily cron
keyword, this will do a daily log file cleanup using cleanup-logs shell
scriptat 00:00 on every day.
@daily
/home/ramesh/arch-linux/bin/cleanup-logs "day started"
11.
How to Execute a Linux Command After Every Reboot using @reboot?
Using the @reboot cron
keyword, this will execute the specified command once after the machine got
booted every time.
@reboot
CMD
12.
How to Disable/Redirect the Crontab Mail Output using MAIL keyword?
By default crontab sends the job
output to the user who scheduled the job. If you want to redirect the output to
a specific user, add or update the MAIL variable in the crontab as shown below.
ramesh@dev-db$
crontab -l
MAIL="ramesh"
@yearly
/home/ramesh/annual-maintenance
*/10
* * * * /home/ramesh/check-disk-space
[Note:
Crontab of the current logged in user with MAIL variable]
If you wanted the mail not to be sent to anywhere, i.e to stop the crontab
output to be emailed, add or update the MAIL variable in the crontab as shown
below.
MAIL=""
13.
How to Execute a Linux Cron Jobs Every Second Using Crontab.
You cannot schedule a every-second
cronjob. Because in cron the minimum unit you can specify is minute. In a
typical scenario, there is no reason for most of us to run any job every second
in the system.
14.
Specify PATH Variable in the Crontab
All the above examples we specified
absolute path of the Linux command or the shell-script that needs to be
executed.
For example, instead of specifying /home/ramesh/tape-backup, if you want to
just specify tape-backup, then add the path /home/ramesh to the PATH variable
in the crontab as shown below.
ramesh@dev-db$
crontab -l
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/ramesh
@yearly
annual-maintenance
*/10
* * * * check-disk-space
[Note:
Crontab of the current logged in user with PATH variable]
15.
Installing Crontab From a Cron File
Instead of directly editing the
crontab file, you can also add all the entries to a cron-file first. Once you
have all thoese entries in the file, you can upload or install them to the cron
as shown below.
ramesh@dev-db$
crontab -l
no
crontab for ramesh
$
cat cron-file.txt
@yearly
/home/ramesh/annual-maintenance
*/10
* * * * /home/ramesh/check-disk-space
ramesh@dev-db$
crontab cron-file.txt
ramesh@dev-db$
crontab -l
@yearly
/home/ramesh/annual-maintenance
*/10
* * * * /home/ramesh/check-disk-space
Note: This will install the cron-file.txt to your crontab, which
will also remove your old cron entries. So, please be careful while uploading
cron entries from a cron-file.txt.
service command examples
Service command is used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, you can use the service command. Check the status of a service:# service ssh statusCheck the steatus of all the services.
service --status-allRestart a service.
# service ssh restart
ps command examples
ps command is used to display information about the processes that are running in the system. While there are lot of arguments that could be passed to a ps command, following are some of the common ones. To view current running processes.$ ps -ef | moreTo view current running processes in a tree structure. H option stands for process hierarchy.
$ ps -efH | more
free command examples
This command is used to display the free, used, swap memory available in the system. Typical free command output. The output is displayed in bytes.$ free
total used free shared buffers cached
Mem: 3566408 1580220 1986188 0 203988 902960
-/+ buffers/cache: 473272 3093136
Swap: 4000176 0 4000176If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
$ free -g
total used free shared buffers cached
Mem: 3 1 1 0 0 0
-/+ buffers/cache: 0 2
Swap: 3 0 3If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.
ramesh@ramesh-laptop:~$ free -t
total used free shared buffers cached
Mem: 3566408 1592148 1974260 0 204260 912556
-/+ buffers/cache: 475332 3091076
Swap: 4000176 0 4000176
Total: 7566584 1592148 5974436
TOP command with examples
1. Show Processes Sorted by any Top Output Column – Press O
By default top command displays the processes in the order of CPU usage. When the top command is running, press M (upper-case) to display processes sorted by memory usage as shown below.
Fig: Press M to sort
by memory usage – Unix top command
Current Sort Field: P for window 1:Def
Select sort field via field letter, type any other key to return
a: PID = Process Id v: nDRT = Dirty Pages count
d: UID = User Id y: WCHAN = Sleeping in Function
e: USER = User Name z: Flags = Task Flags
........When the linux top command is running, Press R, which does the sort in reverse order.
2. Kill a Task Without Exiting From Top – Press k
Once you’ve located a process that needs to be killed, press ‘k’ which will ask for the process id, and signal to send. If you have the privilege to kill that particular PID, it will get killed successfPID to kill: 1309
Kill
PID 1309 with signal [15]:
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 gagent
5136 root
16 0 38040 14m 9836 S
0 0.2 0:00.39 nautilus
3.
Renice a Unix Process Without Exiting From Top – Press r
Press r, if you want to just change the priority of the process (and
not kill the process). This will ask PID for renice, enter the PID and
priority.
PID
to renice: 1309
Renice
PID 1309 to value:
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 gagent
4.
Display Selected User in Top Output Using top -u
Use top -u to display a specific
user processes only in the top command output.
$
top -u geek
While unix top command is running,
press u which will ask for username as shown below.
Which
user (blank for all): geek
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 gagent
Display
Only Specific Process with Given PIDs Using top -p
Use top -p as shown below to display
specific PIDs.
$
top -p 1309, 1882
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 gagent
5.
Display All CPUs / Cores in the Top Output – Press 1 (one)
Top output by default shows CPU line
for all the CPUs combined together as shown below.
top
- 20:10:39 up 40 days, 23:02, 1
user, load average: 4.97, 2.01, 1.25
Tasks:
310 total, 1 running, 309
sleeping, 0 stopped, 0 zombie
Cpu(s): 0.5%us,
0.7%sy, 0.0%ni, 92.3%id, 6.4%wa,
0.0%hi, 0.0%si, 0.0%st
Press 1 (one), when the top command
is running, which will break the CPU down and show details for all the
individual CPUs running on the system as shown below.
top
- 20:10:07 up 40 days, 23:03, 1
user, load average: 5.32, 2.38, 1.39
Tasks:
341 total, 3 running, 337
sleeping, 0 stopped, 1 zombie
Cpu0 :
7.7%us, 1.7%sy, 0.0%ni, 79.5%id, 11.1%wa, 0.0%hi,
0.0%si, 0.0%st
Cpu1 :
0.3%us, 0.0%sy, 0.0%ni, 94.9%id, 4.7%wa,
0.0%hi, 0.0%si, 0.0%st
Cpu2
: 3.3%us, 0.7%sy,
0.0%ni, 55.7%id, 40.3%wa,
0.0%hi, 0.0%si, 0.0%st
Cpu3
: 5.0%us, 1.0%sy,
0.0%ni, 86.2%id, 7.4%wa, 0.0%hi,
0.3%si, 0.0%st
Cpu4 : 38.5%us,
5.4%sy, 0.3%ni, 0.0%id, 54.8%wa, 0.0%hi,
1.0%si, 0.0%st
Cpu5 : 0.0%us, 0.0%sy,
0.0%ni,100.0%id, 0.0%wa, 0.0%hi,
0.0%si, 0.0%st
Cpu6 :
0.3%us, 0.7%sy, 0.0%ni, 97.3%id, 1.7%wa,
0.0%hi, 0.0%si, 0.0%st
Cpu7 :
5.4%us, 4.4%sy, 0.0%ni, 82.6%id, 7.7%wa,
0.0%hi, 0.0%si, 0.0%st
Cpu8
: 1.7%us, 1.7%sy,
0.0%ni, 72.8%id, 23.8%wa,
0.0%hi, 0.0%si, 0.0%st
6.
Refresh Unix Top Command Output On demand (or) Change Refresh Interval
By default, linux top command
updates the output every 3.0 seconds. When you want to update the output
on-demand, press space bar.
To change the output update
frequency, press d in interactive mode, and enter the time in seconds as shown
below.
Change
delay from 3.0 to: 10
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 gagent
7.
Highlight Running Processes in the Linux Top Command Output – Press z or b
Press z or b, which will highlight
all running process as shown below.


Fig: Ubuntu Linux – top command highlights running process
8.
Display Absolute Path of the Command and its Arguments – Press c
Press c which will show / hide
command absolute path, and arguments as shown below.
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 /usr/sbin/gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 /usr/sbin/gagent
-l 0 -u pre
9.
Quit Top Command After a Specified Number of Iterations Using top -n
Until you press q, top continuously
displays the output. If you would like to view only a certain iteration and
want the top to exit automatically use -n option as shown below.
The following example will show 2
iterations of unix top command output and exit automatically
$
top -n 2
10.
Executing Unix Top Command in Batch Mode
If you want to execute top command
in the batch mode use option -b as shown below.
$
top -b -n 1
.
11.
Split Top Output into Multiple Panels – Press A
To display multiple views of top
command output on the terminal, press A. You can cycle through these windows
using ‘a’. This is very helpful, when you can sort the output on multiple
windows using different top output columns.
12.
Get Top Command Help from Command Line and Interactively
Get a quick command line option help
using top -h as shown below.
$
top -h
top: procps version 3.2.0
usage: top -hv | -bcisS -d delay -n iterations [-u
user | -U user] -p pid [,pid ...]
Press h while top command is
running, which will display help for interactive top commands.
Help
for Interactive Commands - procps version 3.2.0
Window
1:Def: Cumulative mode Off. System:
Delay 3.0 secs; Secure mode Off.
Z,B
Global: 'Z' change color mappings; 'B' disable/enable bold
l,t,m
Toggle Summaries: 'l' load avg; 't' task/cpu stats; 'm' mem info
1,I
Toggle SMP view: '1' single/separate states; 'I' Irix/Solaris mode
..........
13.
Decrease Number of Processes Displayed in Top Output – Press n
Press n in the Interactive mode,
which prompts for a number and shows only that. Following example will display
only 2 process as a time.
Maximum
tasks = 0, change to (0 is unlimited): 2
PID USER
PR NI VIRT
RES SHR S %CPU %MEM TIME+
COMMAND
1309 geek
23 0 2483m 1.7g 27m S
0 21.8 45:31.32 gagent
1882 geek
25 0 2485m 1.7g 26m S
0 21.7 22:38.97 gagent
14.
Toggle Top Header to Increase Number of Processes Displayed
By default top displays total number
process based on the window height. If you like to see additional process you
might want to eliminate some of the top header information.
Following is the default header
information provided by top.
top
- 23:47:32 up 179 days, 3:36, 1 user,
load average: 0.01, 0.03, 0.00
Tasks: 67 total,
1 running, 66 sleeping, 0 stopped,
0 zombie
Cpu(s): 0.7% user,
1.2% system, 0.0% nice, 98.0% idle
Mem: 1017136k total, 954652k used, 62484k free, 138280k buffers
Swap: 3068404k total, 22352k used, 3046052k free, 586576k cached
- Press l – to hide / show the load average. 1st header line.
- Press t – to hide / show the CPU states. 2nd and 3rd header line.
- Press m – to hide / show the memory information. 4th and 5th line.
15.
Save Top Configuration Settings – Press W
If you’ve made any interactive top
command configurations suggested in the above examples, you might want to save
those for all future top command output. Once you’ve saved the top
configuration, next time when you invoke the top command all your saved top configuration
options will be used automatically.
To save the top configuration, press
W, which will write the configuration files to ~/.toprc. This will display the
write confirmation message as shown below.
top
- 23:47:32 up 179 days, 3:36, 1 user,
load average: 0.01, 0.03, 0.00
Tasks: 67 total,
1 running, 66 sleeping, 0 stopped,
0 zombie
Cpu(s): 0.7% user,
1.2% system, 0.0% nice, 98.0% idle
Mem: 1017136k total, 954652k used, 62484k free, 138280k buffers
Swap: 3068404k total, 22352k used, 3046052k free, 586576k cached
Wrote
configuration to '/home/ramesh/.toprc'
df command examples
Displays the file system disk space usage. By default df -k displays output in bytes.$ df -k
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 29530400 3233104 24797232 12% /
/dev/sda2 120367992 50171596 64082060 44% /homedf -h displays output in human readable form. i.e size will be displayed in GB’s.
ramesh@ramesh-laptop:~$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 29G 3.1G 24G 12% /
/dev/sda2 115G 48G 62G 44% /homeUse -T option to display what type of file system.
ramesh@ramesh-laptop:~$ df -T
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 ext4 29530400 3233120 24797216 12% /
/dev/sda2 ext4 120367992 50171596 64082060 44% /home
Kill command with examples
1. Kill Command – Kill the process by specifying its PID
All the below kill conventions will send the TERM signal to the specified process. For the signals, either the signal name or signal number can be used. You need to lookup the pid for the process and give it as an argument to kill.$ kill -TERM pid
$ kill -SIGTERM pid
$ kill -15 pid
Example: Kill the firefox process.
$ ps -ef | grep firefox
1986 ? Sl 7:22 /usr/lib/firefox-3.5.3/firefox
$ kill -9 1986
2. Killall Command – Kill processes by name
Instead of specifying a process by its PID, you can specify the name of the process. If more than one process runs with that name, all of them will be killed. Example: Kill all the firefox processes$ killall -9 firefox
3. Pkill Command – Send signal to the process based on its name
You can send signal to any process by specifying the full name or partial name. So there is no need for you to find out the PID of the process to send the signal. Example: Send SIGTERM to all the process which has sample in its name.$ pkill sample
Pkill Example:
Before sending signal, you can verify which are all the process is matching the criteria using “pgrep -l”, which displays the process ID and process name of the matching processes. In this example, all the processes are designed to log the signal to signal-log, along with its PID.$ pgrep -l sample
12406 sample-server.p
12425 sample-server.p
12430 sample-garbagec
$ pkill -USR1 sample
$ cat signal-log
Name: ./sample-server.pl Pid: 12406 Signal Received: USR1
Name: ./sample-server.pl Pid: 12425 Signal Received: USR1
Name: ./sample-garbagecollector.pl Pid: 12430 Signal Received: USR1Note: The part of name which you specify should be in the character within the first 15 character of the process name.
4. Xkill Command – kill a client by X resource
xkill is the simplest way to kill a malfunctioning program. When you want to kill a process, initiate xkill which will offer an cross-hair cursor. Click on the window with left cursor which will kill that process.$ xkill
Select the window whose client you wish to kill with button 1....
xkill: killing creator of resource 0x1200003Note: Actually, xkill instructs XServer to terminate the client.
rm command examples
Get confirmation before removing the file.$ rm -i filename.txtIt is very useful while giving shell metacharacters in the file name argument. Print the filename and get confirmation before removing the file.
$ rm -i file*Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
$ rm -r example
cp command examples
Copy file1 to file2 preserving the mode, ownership and timestamp.$ cp -p file1 file2Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ cp -i file1 file2
mv command examples
Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.$ mv -i file1 file2Note: mv -f is just the opposite, which will overwrite file2 without prompting. mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.
$ mv -v file1 file2
cat command examples
You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.$ cat file1 file2While displaying the file, following cat -n command will prepend the line number to each line of the output.
$ cat -n /etc/logrotate.conf
1 /var/log/btmp {
2 missingok
3 monthly
4 create 0660 root utmp
5 rotate 1
6 }
mount command examples
To mount a file system, you should first create a directory and mount it as shown below.# mkdir /u01
# mount /dev/sdb1 /u01You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
/dev/sdb1 /u01 ext2 defaults 0 2
Following are the symbolic
representation of three different roles:
- u is for user,
- g is for group,
- and o is for others.
Following are the symbolic
representation of three different permissions:
- r is for read permission,
- w is for write permission,
- x is for execute permission.
Following are few examples on how to
use the symbolic representation on chmod.
1.
Add single permission to a file/directory
Changing permission to a single set.
+ symbol means adding permission. For example, do the following to give execute
permission for the user irrespective of anything else:
$
chmod u+x filename
2.
Add multiple permission to a file/directory
Use comma to separate the multiple
permission sets as shown below.
$
chmod u+r,g+x filename
3.
Remove permission from a file/directory
Following example removes read and
write permission for the user.
$
chmod u-rx filename
4.
Change permission for all roles on a file/directory
Following example assigns execute
privilege to user, group and others (basically anybody can execute this file).
$ chmod a+x filename
5. Make permission for a file same as another file (using reference)
If you want to change a file permission same as another file, use the reference option as shown below. In this example, file2′s permission will be set exactly same as file1′s permission.$ chmod --reference=file1 file2
6. Apply the permission to all the files under a directory recursively
Use option -R to change the permission recursively as shown below.$ chmod -R 755 directory-name/
7. Change execute permission only on the directories (files are not affected)
On a particular directory if you have multiple sub-directories and files, the following command will assign execute permission only to all the sub-directories in the current directory (not the files in the current directory).$ chmod u+X *
Note: If the files has
execute permission already for either the group or others, the above command
will assign the execute permission to the user
chown command examples
chown command is used to change the owner and group of a file. \ To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.$ chown oracle:dba dbora.shUse -R to change the ownership recursively.
$ chown -R oracle:dba /home/oracle
passwd command examples
Change your password from command line using passwd. This will prompt for the old password followed by the new password.$ passwdSuper user can use passwd command to reset others password. This will not prompt for current password of the user.
# passwd USERNAMERemove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
# passwd -d USERNAME
mkdir command examples
Following example creates a directory called temp under your home directory.$ mkdir ~/tempCreate nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
$ mkdir -p dir1/dir2/dir3/dir4/
Ifconfig command with examples
Ifconfig command is used to configure network interfaces. ifconfig stands
for interface configurator. Ifconfig is widely used to initialize the network
interface and to enable or disable the interfaces.
In this article, let us review 7 common usages of ifconfig command.
1. View Network Settings of an Ethernet Adapter
Ifconfig, when invoked with no arguments will display all the details of currently active interfaces. If you give the interface name as an argument, the details of that specific interface will be displayed.# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:2D:32:3E:39:3B
inet addr:192.168.2.2 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::21d:92ff:fede:499b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:977839669 errors:0 dropped:1990 overruns:0 frame:0
TX packets:1116825094 errors:8 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2694625909 (2.5 GiB) TX bytes:4106931617 (3.8 GiB)
Interrupt:185 Base address:0xdc00
2. Display Details of All interfaces Including Disabled Interfaces
# ifconfig -a
3. Disable an Interface
# ifconfig eth0 down
4. Enable an Interface
# ifconfig eth0 up
5. Assign ip-address to an Interface
Assign 192.168.2.2 as the IP address for the interface eth0.# ifconfig eth0 192.168.2.2Change Subnet mask of the interface eth0.
# ifconfig eth0 netmask 255.255.255.0Change Broadcast address of the interface eth0.
# ifconfig eth0 broadcast 192.168.2.255Assign ip-address, netmask and broadcast at the same time to interface eht0.
# ifconfig eth0 192.168.2.2 netmask 255.255.255.0 broadcast 192.168.2.255
6. Change MTU
This will change the Maximum transmission unit (MTU) to XX. MTU is the maximum number of octets the interface is able to handle in one transaction. For Ethernet the Maximum transmission unit by default is 1500.# ifconfig eth0 mtu XX7. Promiscuous mode
By default when a network card receives a packet, it checks whether the packet belongs to itself. If not, the interface card normally drops the packet. But in promiscuous mode, the card doesn’t drop the packet. Instead, it will accept all the packets which flows through the network card. Superuser privilege is required to set an interface in promiscuous mode. Most network monitor tools use the promiscuous mode to capture the packets and to analyze the network traffic. Following will put the interface in promiscuous mode.# ifconfig eth0 promiscFollowing will put the interface in normal mode.# ifconfig eth0 -promiscuname command examplesUname command displays important information about the system such as — Kernel name, Host name, Kernel release number, Processor type, etc.,Sample uname output from a Ubuntu laptop is shown below.$ uname -aLinux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linuxwhereis command examplesWhen you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command.$ whereis lsls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gzWhen you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.$ whereis -u -B /tmp -f lsmklsmk: /tmp/lsmkwhatis command examplesWhatis command displays a single line description about a command.$ whatis lsls (1) - list directory contents$ whatis ifconfigifconfig (8) - configure a network interfacelocate command examplesUsing locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.The example below shows all files in the system that contains the word crontab in it.$ locate crontab/etc/anacrontab/etc/crontab/usr/bin/crontab/usr/share/doc/cron/examples/crontab2english.pl.gz/usr/share/man/man1/crontab.1.gz/usr/share/man/man5/anacrontab.5.gz/usr/share/man/man5/crontab.5.gz/usr/share/vim/vim72/syntax/crontab.vimman command examplesDisplay the man page of a specific command.$ man crontabWhen a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below.$ man SECTION-NUMBER commandnameFollowing 8 sections are available in the man page.
- General commands
- System calls
- C library functions
- Special files (usually devices, those found in /dev) and drivers
- File formats and conventions
- Games and screensavers
- Miscellaneous
- System administration commands and daemons
For example, when you do whatis crontab, you’ll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.$ whatis crontabcrontab (1) - maintain crontab files for individual users (V3)crontab (5) - tables for driving cron$ man 5 crontabtail command examplesPrint the last 10 lines of a file by default.$ tail filename.txtPrint N number of lines from the file named filename.txt$ tail -n N filename.txtView the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.$ tail -f log-file1. Less Command – Search NavigationOnce you’ve opened a log file (or any file) using less file-name, use the following keys to search. Please note that the match will be highlighted automatically by default.Forward Search
- / – search for a pattern which will take you to the next occurrence.
- n – for next match in forward
- N – for previous match in backward
Backward
Search
- ? – search for a pattern which will take you to the previous occurrence.
- n – for next match in backward direction
- N – for previous match in forward direction
Tip: If you dont bother about which direction the search is
happening, and you want to search file path, or URL, such as “/home/ramesh/”,
you can use backward search (?pattern) which will be handy as you don’t want to
escape slashes each time.
Search
Path
In
forward: /\/home\/ramesh\/
In
backward: ?/home/ramesh/
2.
Less Command – Screen Navigation
Use the following screen navigation
commands while viewing large log files.
- CTRL+F – forward one window
- CTRL+B – backward one window
- CTRL+D – forward half window
- CTRL+U – backward half window
3.
Less Command – Line navigation
In a smaller chunk of data, where
you want to locate particular error, you may want to navigate line by line
using these keys:
- j – navigate forward by one line
- k – navigate backward by one line
4.
Less Command – Other Navigations
The following are other navigation
operations that you can use inside the less pager
- G – go to the end of file
- g – go to the start of file
- q or ZZ – exit the less pager
5.
Simulate tail -f inside less pager – Press F
Once you’ve opened a file using less
command, any content that is appended to the file after that will not be
displayed automatically. However, you can press F less command will show
the status ‘waiting for data‘. This is as similar to ‘tail -f
6.
Less Command – Count magic
Similar to Vim editor navigation
command, you can give 10j to scroll 10 lines down, or 10k to go up by 10 lines.
- 10j – 10 lines forward.
- 10k – 10 lines backward.
- CTRL+G – show the current file name along with line, byte and percentage statistics.
7.
Other useful Less Command Operations
- v – using the configured editor edit the current file.
- h – summary of less commands
- &pattern – display only the matching lines, not all.
9.
Less Command – Marked navigation
When you are viewing a large log
file using less command, you can mark a particular position and return back to
that place again by using that mark.
- ma – mark the current position with the letter ‘a’,
- ‘a – go to the marked position ‘a’.
10.
Less Command – Multiple file paging
Method 1: You can open multiple files by passing the file names as
arguments.
$
less file1 file2
Method 2: While you are viewing file1, use :e to open the file2 as
shown below.
$
less file1
:e
file2
Navigation across files: When you opened more than two files ( for e.g – less * ),
use the following keys to navigate between files.
- :n – go to the next file.
- :p – go to the previous file.
su command examples
Switch to a different user account using su command. Super user can switch to any other user without entering their password.$ su - USERNAMEExecute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.
[john@dev-server]$ su - raj -c 'ls'
[john@dev-server]$Login to a specified user account, and execute the specified shell instead of the default shell.
$ su -s 'SHELLNAME' USERNAME
mysql command examples
mysql is probably the most widely used open source database on Linux. Even if you don’t run a mysql database on your server, you might end-up using the mysql command ( client ) to connect to a mysql database running on the remote server. To connect to a remote mysql database. This will prompt for a password.$ mysql -u root -p -h 192.168.1.2To connect to a local mysql database.
$ mysql -u root -p
If you want to specify the mysql root
password in the command line itself, enter it immediately after -p (without any
space).
46. yum command examples
To install apache using yum.$ yum install httpdTo upgrade apache using yum.
$ yum update httpdTo uninstall/remove apache using yum.
$ yum remove httpd
Rpm command with examples
RPM command is used for installing, uninstalling,
upgrading, querying, listing, and checking RPM packages on your Linux system.
RPM stands for Red Hat Package
Manager.
With root privilege, you can use the
rpm command with appropriate options to manage the RPM software packages.
In this article, let us review 15 practical examples of rpm command.
Let us take an rpm of Mysql Client
and run through all our examples.
1.
Installing a RPM package Using rpm -ivh
RPM filename has packagename,
version, release and architecture name.
For example, In the
MySQL-client-3.23.57-1.i386.rpm file:
- MySQL-client – Package Name
- 3.23.57 – Version
- 1 – Release
- i386 – Architecture
When you install a RPM, it checks
whether your system is suitable for the software the RPM package contains,
figures out where to install the files located inside the rpm package, installs
them on your system, and adds that piece of software into its database of
installed RPM packages.
The following rpm command installs
Mysql client package.
#
rpm -ivh MySQL-client-3.23.57-1.i386.rpm
Preparing...
########################################### [100%]
1:MySQL-client
########################################### [100%]
rpm command and options
- -i : install a package
- -v : verbose
- -h : print hash marks as the package archive is unpacked.
2.
Query all the RPM Packages using rpm -qa
You can use rpm command to query all
the packages installed in your system.
#
rpm -qa
cdrecord-2.01-10.7.el5
bluez-libs-3.7-1.1
setarch-2.0-1.1
.
.
- -q query operation
- -a queries all installed packages
To identify whether a particular rpm
package is installed on your system, combine rpm and grep command as shown
below. Following command checks whether cdrecord package is installed on your
system.
#
rpm -qa | grep 'cdrecord'
3.
Query a Particular RPM Package using rpm -q
The above example lists all
currently installed package. After installation of a package to check the
installation, you can query a particular package and verify as shown below.
#
rpm -q MySQL-client
MySQL-client-3.23.57-1
#
rpm -q MySQL
package
MySQL is not installed
Note: To query a package, you should
specify the exact package name. If the package name is incorrect, then rpm
command will report that the package is not installed.
4.
Query RPM Packages in a various format using rpm –queryformat
Rpm command provides an option
–queryformat, which allows you to give the header tag names, to list the
packages. Enclose the header tag with in {}.
#
rpm -qa --queryformat '%{name-%{version}-%{release} %{size}\n'
cdrecord-2.01-10.7
12324
bluez-libs-3.7-1.1
5634
setarch-2.0-1.1
235563
.
.
#
5.
Which RPM package does a file belong to? – Use rpm -qf
Let us say, you have list of files
and you would want to know which package owns all these files. rpm command has
options to achieve this.
The following example shows that
/usr/bin/mysqlaccess file is part of the MySQL-client-3.23.57-1 rpm.
#
rpm -qf /usr/bin/mysqlaccess
MySQL-client-3.23.57-1
- -f : file name
6.
Locate documentation of a package that owns file using rpm -qdf
Use the following to know the list
of documentations, for a package that owns a file. The following command, gives
the location of all the manual pages related to mysql package.
#
rpm -qdf /usr/bin/mysqlaccess
/usr/share/man/man1/mysql.1.gz
/usr/share/man/man1/mysqlaccess.1.gz
/usr/share/man/man1/mysqladmin.1.gz
/usr/share/man/man1/mysqldump.1.gz
/usr/share/man/man1/mysqlshow.1.gz
- -d : refers documentation.
7.
Information about Installed RPM Package using rpm -qi
rpm command provides a lot of
information about an installed pacakge using rpm -qi as shown below:
#
rpm -qi MySQL-client
Name : MySQL-client Relocations: (not relocatable)
Version : 3.23.57 Vendor: MySQL AB
Release : 1 Build Date: Mon 09
Jun 2003 11:08:28 PM CEST
Install
Date: Mon 06 Feb 2010 03:19:16 AM PST Build Host: build.mysql.com
Group : Applications/Databases Source RPM: MySQL-3.23.57-1.src.rpm
Size : 5305109 License: GPL / LGPL
Signature : (none)
Packager : Lenz Grimmer
URL : http://www.mysql.com/
Summary : MySQL - Client
Description
: This package contains the standard MySQL clients.
If you have an RPM file that you
would like to install, but want to know more information about it before
installing, you can do the following:
#
rpm -qip MySQL-client-3.23.57-1.i386.rpm
Name : MySQL-client Relocations: (not relocatable)
Version : 3.23.57 Vendor: MySQL AB
Release : 1 Build Date: Mon 09
Jun 2003 11:08:28 PM CEST
Install
Date: (not installed) Build
Host: build.mysql.com
Group : Applications/Databases Source RPM: MySQL-3.23.57-1.src.rpm
Size : 5305109 License: GPL / LGPL
Signature : (none)
Packager : Lenz Grimmer
URL : http://www.mysql.com/
Summary : MySQL - Client
Description
: This package contains the standard MySQL clients.
- -i : view information about an rpm
- -p : specify a package name
8.
List all the Files in a Package using rpm -qlp
To list the content of a RPM
package, use the following command, which will list out the files without
extracting into the local directory folder.
$
rpm -qlp ovpc-2.1.10.rpm
/usr/bin/mysqlaccess
/usr/bin/mysqldata
/usr/bin/mysqlperm
.
.
/usr/bin/mysqladmin
- q : query the rpm file
- l : list the files in the package
- p : specify the package name
9.
List the Dependency Packages using rpm -qRP
To view the list of packages on which
this package depends,
#
rpm -qRp MySQL-client-3.23.57-1.i386.rpm
/bin/sh
/usr/bin/perl
10.
Find out the state of files in a package using rpm -qsp
The following command is to find
state (installed, replaced or normal) for all the files in a RPM package.
#
rpm -qsp MySQL-client-3.23.57-1.i386.rpm
normal /usr/bin/msql2mysql
normal /usr/bin/mysql
normal /usr/bin/mysql_find_rows
normal /usr/bin/mysqlaccess
normal /usr/bin/mysqladmin
normal /usr/bin/mysqlbinlog
normal /usr/bin/mysqlcheck
normal /usr/bin/mysqldump
normal /usr/bin/mysqlimport
normal /usr/bin/mysqlshow
normal /usr/share/man/man1/mysql.1.gz
normal /usr/share/man/man1/mysqlaccess.1.gz
normal /usr/share/man/man1/mysqladmin.1.gz
normal /usr/share/man/man1/mysqldump.1.gz
normal /usr/share/man/man1/mysqlshow.1.gz
11.
Verify a Particular RPM Package using rpm -Vp
Verifying a package compares
information about the installed files in the package with information about the
files taken from the package metadata stored in the rpm database. In the
following command, -V is for verification and -p option is used to specify a
package name to verify.
#
rpm -Vp MySQL-client-3.23.57-1.i386.rpm
S.5....T
c /usr/bin/msql2mysql
S.5....T
c /usr/bin/mysql
S.5....T
c /usr/bin/mysql_find_rows
S.5....T
c /usr/bin/mysqlaccess
The character in the above output
denotes the following:
- S file Size differs
- M Mode differs (includes permissions and file type)
- 5 MD5 sum differs
- D Device major/minor number mismatch
- L readlink(2) path mismatch
- U User ownership differs
- G Group ownership differs
- T mTime differs
12.
Verify a Package Owning file using rpm -Vf
The following command verify the package
which owns the given filename.
#
rpm -Vf /usr/bin/mysqlaccess
S.5....T
c /usr/bin/mysql
#
13.
Upgrading a RPM Package using rpm -Uvh
Upgrading a package is similar to
installing one, but RPM automatically un-installs existing versions of the
package before installing the new one. If an old version of the package is not
found, the upgrade option will still install it.
#
rpm -Uvh MySQL-client-3.23.57-1.i386.rpm
Preparing...
###########################################
[100%]
1:MySQL-client ###########################################
14.
Uninstalling a RPM Package using rpm -e
To remove an installed rpm package
using -e as shown below. After uninstallation, you can query using rpm -qa and
verify the uninstallation.
#
rpm -ev MySQL-client
15.
Verifying all the RPM Packages using rpm -Va
The following command verifies all
the installed packages.
#
rpm -Va
S.5....T
c /etc/issue
S.5....T
c /etc/issue.net
S.5....T
c /var/service/imap/ssl/seed
S.5....T
c /home/httpd/html/horde/ingo/config/backends.php
.
.
S.5....T
c /home/httpd/html/horde/ingo/config/prefs.php
S.5....T c
/etc/printcap
Ping command with examples
Ping Example 1. Increase or Decrease the Time Interval Between Packets
By default ping waits for 1 second before sending the next packet. You can increase or decrease this using option -i as shown below.Increase Ping Time Interval
Example: Wait for 5 seconds before sending the next packet.$ ping -i 5 IP
Decrease Ping Time Interval
Example: Wait 0.1 seconds before sending the next packet.date command examples
Set the system date:# date -s "01/31/2010 23:59:53"Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
# hwclock –systohc
# hwclock --systohc –utc
wget command examples
The quick and effective method to download software, music, video from internet is using wget command.$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gzDownload and store it with a different name.
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701
No comments:
Post a Comment