1. grep
Search for a given string in a file (case in-sensitive search).$ grep -i "the" demo_file
Print the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Search for a given string in all files recursively
$ grep -r "ramesh" *
2. tar
Create a new tar archive.
$ tar cvf archive_name.tar dirname/
Extract from an existing tar archive.
$ tar xvf archive_name.tar
View an existing tar archive.
$ tar tvf archive_name.tar
3. find
Find files using file-name ( case in-sensitve find) # find -iname "MyCProgram.c"
Execute commands on files found by the find command
$ find -iname "MyCProgram.c" -exec md5sum {} \;
Find all empty files in home directory
# find ~ -empty
4. ssh
Login to remote hostssh -l jsmith remotehost.example.com
Debug ssh client
ssh -v -l jsmith remotehost.example.com
Display ssh client version
$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003
5. sort
Sort a file in ascending order$ sort names.txt
Sort a file in descending order
$ sort -r names.txt
Sort passwd file by 3rd field.
$ sort -t: -k 3n /etc/passwd | more
6. ls
Display filesize in human readable format (e.g. KB, MB etc.,)$ ls -lh -rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz
Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
$ ls -ltr
Visual Classification of Files With Special Characters Using ls -F
$ ls -F
7. pwd
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.8. cd
Use “cd -” to toggle between the last two directoriesUse “shopt -s cdspell” to automatically correct mistyped directory names on cd
9. gzip
To create a *.gz compressed file:$ gzip test.txt
To uncompress a *.gz file:
$ gzip -d test.txt.gz
Display 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
10 . unzip
To extract a *.zip compressed file:$ unzip test.zip
View 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_
11. shutdown
Shutdown the system and turn the power off immediately.# shutdown -h now Shutdown the system after 10 minutes.
# shutdown -h +10 Reboot the system using shutdown command.
# shutdown -r now Force the filesystem check during reboot.
# shutdown -Fr now
12. ps
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 | more
To view current running processes in a tree structure. H option stands for process hierarchy.
$ ps -efH | more
13. free
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.
14. top
top command displays the top processes in the system ( by default sorted by cpu usage ). To sort top output by any column, Press O (upper-case O)15. kill
Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process. $ ps -ef | grep vim
ramesh 7243 7222 9 22:43 pts/2 00:00:00 vim
$ kill -9 7243
16. rm
Get confirmation before removing the file.$ rm -i filename.txt It 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.
17. cp
Copy file1 to file2 preserving the mode, ownership and timestamp.$ cp -p file1 file2
Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ cp -i file1 file2
18. mv c
Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.$ mv -i file1 file2
Note: 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
19. cat
You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.$ cat file1 file2
20. chmod
chmod command is used to change the permissions for a file or directory.Give full access to user and group (i.e read, write and execute ) on a specific file.
$ chmod ug+rwx file.txt Revoke all access for the group (i.e read, write and execute ) on a specific file.
$ chmod g-rwx file.txt Apply the file permissions recursively to all the files in the sub-directories.
$ chmod -R ug+rwx file.txt