Top 10 Linux Commands for Every SysAdmins Should Know



In the world of IT if the SysAdmin doesn't know how to work on the terminal (Linux / Unix CLI) then they are not real SysAdmins. There are many commands for the SysAdmin to know and work around but few are very important. I am skipping the basic commands like vim, ls and mv etc. This post is more towards the real objective of the SysAdmin to work on the servers (Linux / Unix ) without Gui. 

Below are the list of top 10 commands which every SysAdmin should know and also how to use it
  • cat
  • tail / head
  • touch
  • ps
  • grep 
  • awk 
  • sed
  • locate
  • chmod / chown
  • top / htop

- Cat

The cat (short for “ concatenate “) command is one of the most frequently used command in Linux/Unix like operating systems. This command allows us to create single or multiple files, view contain of file, concatenate files and redirect output in terminal or files

Syntax: cat [Option] [Filename]

Examples:

# cat –n info.txt (Show the file content with lines)

1 Hello World

# cat > testfile (file will be created and input the in the file)
#cat testfile > testfile1 (Redirect  standard output to the file)


Tail / Head

Tail is the program of Linux / Unix used to display the end of the text file or piped data. By default, it shows last 10 lines

Syntax: tail [options] [filename]

Head

Head is the program of Linux / Unix used to display the top of the text file or piped data. By default, it shows top 10 lines.

Syntax: head [options] [filename]

Examples:

#tail –n 20 filename.txt (Display the last 20 lines of the file)
#tail –f file.log (Display the outdated logs on the file)

#head –n 20 filename.txt (Display the top 20 lines of the file)
#ls | head (show the all files/folder in align order)


- Touch

Touch command is the easiest way to create new, empty and many files. It is also used to change the timestamp on the existing files and directories

Syntax: touch [Options] [Filenames]

Examples:

#touch files1 file2 file3 (Created the 3 files)
#touch –d ‘1 Aug 2018 10:11’ file1

- PS

The ps (processes status) command display the currently-running processes.

Syntax: ps [Options]

Examples:

#ps –A (Display all active processes)
#ps –fL –C httpd (Display all thread of the process named httpd)
#ps –U root –u root (Print all processes running as root)

- Grep

The grep command which stands for “global regular expression print,” processes text line by line and prints any lines which match a specified pattern. The grep command is used to search text or searches the given file for lines containing a match to the given strings or words.

Syntax: grep [Options] [Pattern] [file…]

Examples:

#grep 'test' file1 file2 file3 (Find the word ‘test’ from these files)
#grep –R ‘httpd’ /etc (Find the work httpd recursively from the dir etc)
#grep –w “wheel” /etc/passwd (Find the word wheel in the path)


- Awk

The awk command or GNU awk in specific provides a scripting language for text processing. The awk command programming language requires no compiling, and allows the user to use variables, numeric functions, string functions, and logical operators.

Syntax: awk [Options] [file…]

Examples:
#cat file
This is my file
END

#awk ‘{print $1}’ file (Display the first field of the file)
This
END

#awk -F: '{print $1,$6}' /etc/passwd (Display the passwd field values in which service and its path)

gdm /var/lib/gdm
ntp /etc/ntp
apache /var/www

- Sed

SED command in Linux / UNIX is stands for stream editor and it can perform function on file like, searching, find and replace, insertion or deletion. Most common use of SED command is substitution or for find and replace. By using SED you can edit files even without opening it, which is much quicker way to find and replace something in file, than first opening that file in VI Editor and then changing it.

Syntax: sed [Options] [Script] [Input]

Examples:

#cat file
This is test file
This is test2 file
END

#sed ‘s/test/new test/’ ./file  (Display the Stdin output like below )

This is new test file
This is test2 file
END


#sed ‘s/test/oldtest new/w file2’ ./file (It will save the output to the new file)

#sed ‘2d’ file (It will delete the second line)

This is test file
END

- Locate

The two most popular file searching utilities are find and locate. The locate utility work faster than find because when find command initiated it search the file system but locate command search on the database.

Syntax: find [File..]
Syntax: locate [File..]

#locate java –n 20 (Display the 20 lines of keyword java)
#find . –name output.txt (Display the output.txt from the whole dir and sub-dir)

- Chmod / Chown

chown defines who owns the file (Owner , groups)
chmod defines who can do what (Permission: Read – Write – Execute)

Examples:

#ls –l file
-rwxrwxr-x  2  root users   26 May  18 12:49 file

#chown root:users testfile (The file named testfile has owner root,Group is users )
 Read=4 Write=2 Execute=1


#chmod 775 testfile (The testfile owner is all permission, group also all permission and others has Read and execute) [ -R = used for recursively ]


- Top / Htop 

The top command is used to show all the running processes within your Linux / Unix box.

#top
#top -u admin (Display the all processes that admin user is running )

Htop is same like Top - linux process monitoring but it not builtin installed utility. We have to install it with package manager to use it. But it is more advanced and have rich features like interface to manage process,shortcuts key to filter process tree etc.

Bonus 

- Netstat

Netstat is a command line tool for monitoring incoming and outgoing network packets statistics as well as interface statistics. It is very useful tool to monitor network performance and troubleshoot network related problems.

#netstat -a | more (Display all listening ports (TCP and UDP))






Comments