Linux Commands Tips and Tricks

This post update every day! You can follow this article and learn linux command tips and tricks. Enjoy :).

1. tar command examples

Create 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

2.grep command examples

Search for a given string in a file (case in-sensitive search).

$ grep -i "the" demo_files

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 "pooya" *

3.find command examples

Find files using file-name ( case in-sensitve find)

# find -iname "MyCProgram.c"

Find all empty files in home directory

# find ~ -empty

4. ssh command examples

Login to remote host

ssh -l jsmith

Debug ssh client

ssh -v -l jsmith

Display ssh client version

$ ssh -V
OpenSSH_7.2p2 Ubuntu-1, OpenSSL 1.0.2g  1 Mar 2016

5. Vim (Vim is an advanced text editor)

Go to the 143rd line of file

$ vim +143 filename.txt

Go to the first match of the specified

$ vim +/search-term filename.txt

Open the file in read only mode.

$vim -R /etc/passwd

6. diff command

Ignore white space while comparing.

# diff -w name_list.txt name_list_new.txt

< John Doe --- > John M Doe
> Jason Bourne

7. ls command (list directory contents)

Display filesize in human readable format (e.g. KB, MB etc.,)

[email protected]:~$ ls -lh
total 1.8G
drwxrwxrwx 13 pooya pooya 4.0K Sep 21  2015 android-sdks
drwxrwxr-x  7 pooya pooya 4.0K Aug 19  2015 android-studio
drwxrwxrwx  3 pooya pooya 4.0K Mar 19 11:14 design
drwxr-xr-x  2 pooya pooya 4.0K Mar 21 01:52 Desktop
drwxr-xr-x  2 pooya pooya 4.0K Mar 19 11:02 Documents
drwxr-xr-x  3 pooya pooya 4.0K Mar 23 21:36 Downloads
-rwxrwxrwx  1 pooya pooya 8.8K Feb 25 23:28 examples.desktop
comments powered by Disqus