Linux Command line is one of the powerful tools for admins. It is also known as Terminal, Shell, Command-line interfaces ( CLI ). Commands are an easier way to reach a specific path on Linux using CLI and also useful for the execution of multiple files or software.
If you just started learning with any Linux distribution or any ubuntu flavor or if you have good experience I am sure this tutorial is going to be worth with best commands covering entire ubuntu commands end to end. So, Let’s get started.
Table of Content
- Working with commands to Manage users and groups
- Working with commands to Manage running programs
- Working with commands for your daily use
- What is Stream Editor (sed) and how to use it ?
Working with commands to Manage users and groups
I will show you some of the most important commands which manages both users and groups.
- chage: To modify the user password expiry
- chfn: It allows you to change user name and details( change finger )
- chsh: It allows user to change the Login Shell
- groupmod: To modify the group properties such as name of group
groupmod <new-name> <old-name>
- groupadd: To add the group in a particular group
- umask: To mask permission the original permissions ie. opposite to actual permissions.
umask 000 # This is equivalent to all permissions read, write and execute
- userdel : To delete a particular user
- useradd: To add a particular user
- passwd: To change the password for a user
- usermod: To modify users permission such as adding into a particular group
usermod -aG sudo <username> # here we are adding user in sudo group
- gpasswd: To administer the group like to add a user
gpasswd -a <user> <group-name>
- mkpasswd: Generates Random Password from /dev/random
- groupdel: To delete a particular group
Working with commands to Manage running programs
- bg: Bring the suspended Job in Background.
sleep 100 then clt+z # To Suspend the Job jobs # To check Jobs bg %1 # Bring stopped Job in background
- fg: Bring the suspended Job in foreground.
sleep 100 & # Run Job in background jobs # To check Jobs fg %1 # Bring background Job in foreground
- jobs: Displays the list of Jobs.
- kill: It is built in command which is used to terminate the process manually
- nice: This command helps in scheduling the priority of a process.
nice -10 shell # Here it sets the priority 10 for shell process.
- ps: Known as process status.
ps -ef | grep systemd # Here we are displaying full format list of running processes
- pstree: Shows Linux running process as tree
- renice: Changes priority of the running process.
renice -n -15 -p 77982 # Here for process 77982 it is setting nice value 15
- top: Real time running process
top -u ubuntu
- watch: Run commands at regular interval .
watch -n 5 df -h # Here we are displaying disk usage every 5 seconds
Working with commands for your daily use
|du||To find the disk usage|
|find||to find a file or files by performing a search|
|less||Filters for paging through output|
|ln||Creates Link between files|
|locate||Finds file from index|
|make||Compiles and installs programs using makefile|
|sort||Generates the output in order|
|which||Location of the command|
|lsblk||To list storage and block devices|
|lshw||To Provide detailed list storage and block devices|
|lsmod||To list the status of modules in the Linux kernel|
|whatis||To provide a line description of command|
|tail||It prints last line of a file|
|wget||It is used to download the file|
|diff||It is used to differentiate between two files|
|comm||To check similarities between two files|
|ps -aux||Lists the Process where a is all , u is users and x is to attach to a terminal|
|top||It provides resource utilization|
|nice/renice||To set priority of the Process|
|ps aux –sort=-%cpu | grep -v ‘whoami’||Pipe commands|
|env or printenv||It displays all the environment variables|
|PATH=$PATH:$HOME/bin||It sets your path by adding Home Directory to your Path|
What is Stream Editor (sed) and how to use it ?
Stream Editor is a command in UNIX and perform lots of functions such as file search, replacement , insertion and deletion. It is also used for substituting or finding the text or files. There are lots of benefits and good features which SED has such as you can edit files without even opening it. It is powerful text stream editor.
Every system admin has to work with File System ie. files and directories regularly and it may take lot of time and efforts to work with them, so instead of working manually and using lots of different command we can use SED which can be very useful and handy. Lets understand how can we use SED commands.
How can you replace a String in a file?
Here we are replacing india with germany everywhere in file but only at first occurrence in each line
sed 's/india/germany/' file.txt
How can you replace the nth occurrence of a pattern in line of a file ?
Here we will replace india with germany on 2nd occurrence in each line everywhere in file
sed 's/india/germany/2' file.txt
How can you replace all the occurrence of the pattern in a line in a file?
Here we will replace india with germany on each occurrence in each line everywhere in file
sed 's/india/germany/g' file.txt
How can we replacing from nth occurrence to all occurrences in a line in a file?
Here we will replace india with germany from 3rd occurrence to last occurrence in each line everywhere in file
sed 's/india/germany/3g' file.txt
How can you replace string on a range of lines in a file ?
Here we will replace india with germany on first occurrence in Line no 1 , 2 and 3
sed '1,3 s/india/germany/' file.txt
Here we will replace india with germany on first occurrence in Line no 2 to Last line($)
sed '2,$ s/india/germany/' file.txt
How can you duplicate replaced line and print ?
Replaced line is printed twice on the terminal. Here we are using p flag to print.
sed 's/india/germany/p' file.txt
How can you print replaced only once ?
Replaced line is printed twice on the terminal. Here we are using p flag to print and n flag to suppresses duplicate rows.
sed -n 's/india/germany/p' file.txt
How to delete a last line in a file?
sed '$d' file.txt
How to delete nth Line in a file?
Here we are deleting 2nd Line in a file
sed '2d' file.txt
How to delete line from range x to y?
Here we are deleting 3rd to 5th Line in a file
sed '3,5d' file.txt
How to extract line from range x to y?
Here we are printing 3rd to 5th Line . Here p denotes print and n flag to suppress the unmatched text
sed -n '3,5p' file.txt
How to extract line from range x to y with multiple selections?
Here we are printing 3rd to 5th Line and 31st to 50th Line. Here p denotes print and n flag to suppress the unmatched text and e to make multiple selections.
sed -n -e '3,5p' -e '31,50p' file.txt
▶ Getting information—apropos, cal, cat, cmp, date, diff, df, dir, dmesg, du, env, file, free, grep, head, info, last, less, locate, ls, lsattr, man, more, pinfo, ps,
pwd, stat, strings, tac, tail, top, uname, uptime, vdir, vmstat, w, wc, whatis, whereis, which, who, whoami
▶ Console text editors—ed, jed, joe, mcedit, nano, red, sed, vim ,
▶ Console Internet and network commands—bing, elm, ftp, host, hostname, ifconfig, links, lynx, mail, mutt, ncftp, netconfig, netstat, pine, ping, pump,
rdate, route, scp, sftp, ssh, tcpdump, traceroute, whois, wire-test