Monday 27 August 2018

Common Linux Commands for Beginners

Similar to the Command Prompt in Windows, Linux has the Terminal in order to help you configure and interact with your system. For someone to work in the Terminal they need to familiarize themselves with Linux commands. Once familiarized it is fairly easy to work from the Terminal and that is why most of the Linux workers prefer the Linux terminal over the GUI.

This article will help you to get familiarized with all the most common Linux commands and their usages. These commands are divided into 15 sections based on their functionalities.

System Related Commands

These commands are used to view and manage Linux system-related information.
1. uname                   :  Displays linux system information. With -a switch you can view all the information, with -r switch you can view kernel release information and with -o you can view OS information
2. cat /etc/redhat_release :  Shows which version of redhat installed 
3. uptime                  :  Shows how long the system has been running
4. hostname                :  Shows system host name. With -i switch you can view the ip address of the machine and with -d you can view the domain name
5. last reboot             :  Shows system reboot history
6. date                    :  Shows the current date and time. You can specify the format you want to view the date as well. As an example, by using 'date +%D' you can view the date in 'MM/DD/YY' format
7. cal                     :  Shows the calendar of the current month. With -y switch you can view the calendar of the whole current year
8. w                       :  Displays who is logged on and what they are doing
9. whoami                  :  Shows current user id
10. finger user            :  Displays information about user
11. reboot                :  Reboots the system
12. shutdown              :  Shuts down the system

Hardware Related Commands

These commands are used to view and manage hardware-related aspects of the Linux machine.
13. dmesg                 : Displays all the messages from Kernel ring buffer. With -k switch you can view kernel messages and with -u you can view userspace messages
14. cat /proc/cpuinfo     : Displays information about processes and CPUs of the system
15. cat /proc/meminfo     : Displays details on hardware memory
16. cat /proc/interrupts  : Lists the number of interrupts per CPU per I/O device
17. lshw                  : Displays information on hardware configuration of the system. But this command must be run as super user or it will only report partial information
18. lsblk                 : Displays block device related information of the machine. With -a you can view all block devices
19. free -m               : Shows used and free memory (-m for MB)
20. lspci -tv             : Shows information on PCI buses devices
21. lsusb -tv             : Shows information on USB devices
22. dmidecode             : Shows hardware info from the BIOS (vendor details)
23. hdparm -i /dev/sda    : Shows info about disk sda
    hdparm -tT /dev/sda   : Performs a read speed test on disk sda
24. badblocks -s /dev/sda : Tests for unreadable blocks on disk sda

Statistic Related Commands

These set of commands are used to view various kinds of stats of the Linux system
25. mpstat 1                      : Displays processors related statistics
26. vmstat 2                      : Displays virtual memory statistics
27. iostat 2                      : Displays I/O statistics
28. tail -n 500 /var/log/messages : Displays the last 500 kernel/syslog messages
29. tcpdump -i eth1               : Captures all packets flow on interface eth1. With -w switch you can specify a file where you can direct the output to
    tcpdump -i eth0 'port 80'     : Monitors all traffic on port 80 on interface eth0
30. lsof                          : Lists all open files belonging to all active processes
    lsof -u testuser              : Lists files opened by a specific user
31. free -m                       : Shows RAM memory details
32. watch df -h                   : Watches changeable disk usage continuously

User-Related Commands

These commands are used to manage Linux users
33. id                                      : Shows the active user and group information. With -G switch you can view the IDs of groups
34. last                                    : Shows a list of last logins on the system. Using -a switch you can add the hostname to the last column of the output
35. who                                     : Shows who is logged on the system
36. groupadd admin                          : Adds the group "admin"
37. useradd -c "Sam Tomshi" -g admin -m sam : Creates user "sam" and adds to group "admin"
38. userdel sam                             : Deletes user sam
39. adduser sam                             : Adds user "sam"
40. usermod                                 : Modifies user information
41. passwd user1                            : Changes the password of user1

File Related Commands

These commands are used to handle files and directories
42. ls -al                                 : Displays all information about files/directories. This includes displaying all hidden files as well
43. pwd                                    : Shows current directory path
44. mkdir directory-name                   : Creates a directory
45. rm file-name                           : Deletes file
    rm -r directory-name                   : Deletes directory recursively 
    rm -f file-name                        : Forcefully removes file
    rm -rf directory-name                  : Forcefully removes directory recursively
46. cp file1 file2                         : Copies linux files, here file1 to file2
    cp -r dir1 dir2                        : Copies dir1 to dir2, creates dir2 if it doesn't  exist
47. mv file1 file2                         : Moves files from one place to another/renames file1 to file2
48. ln -s  /path/to/file-name link-name    : Creates a symbolic link to file-name
49. touch file                             : Creates empty file
50. cat file                               : Prints the file content in terminal
51. more file                              : Display the contents of file
52. head file                              : Display the first 10 lines of file
53. tail file                              : Outputs the last 10 lines of file
    tail -f file                           : Outputs the contents of file as it grows starting with the last 10 lines
54. gpg -c file                            : Encrypts file
    gpg file.gpg                           : Decrypts file
55. cksum file                             : View the checksum of the file
56. diff file1 file2                       : View the differences between contents of file1 and file2
57. ln -s link file                        : Create a soft link named link to the file
58. sort                                   : Sorts files in alphabetical order
59. uniq                                   : Compares adjacent lines in a file and removes/reports any duplicate lines
60. wc                                     : Counts number of words/lines
61. dir                                    : Lists the content of the directory
62. tee                                    : Command for chaining and redirection
63. tr                                     : Command for translating characters

Process Related Commands

These commands are used to handle Linux processes
64. ps                       : Displays your currently active processes
    ps aux | grep 'telnet'   : Displays all process ids related to telnet process
65. pmap                     : Display Memory map of process
66. top                      : Display all running processes and cpu/memory usage
67. kill pid                 : Kills process with mentioned pid
68. killall proc             : Kills all processes named proc
69. pkill processname        : Sends kill signal to a process with its name
70. bg                       : Resumes suspended jobs without bringing them to foreground
71. fg                       : Brings the most recent job to foreground
    fg n                     : Brings job n to the foreground

File Permission Related Commands

These commands are used to change permissions of the files
72. chmod octal file-name             : Changes the permissions of file to octal
    chmod 777 /data/test.c                   : Sets rwx permission for owner , group and others
    chmod 755 /data/test.c                   : Sets rwx permission for owner and rx for group and others
73. chown owner-user file                    : Changes owner of the file
    chown owner-user:owner-group  file-name  : Changes owner and group owner of the file
    chown owner-user:owner-group directory   : Changes owner and group owner of the directory
74. chgrp group1 file                        : Changes the group ownership of the file to group1

Network Related Commands

These commands are used to view and edit network configurations related aspects of the system
75. ifconfig -a        : Displays all network interface and set ip address
76. ifconfig eth0      : Displays eth0 ethernet port ip address and details
77. ip addr show       : Display all network interfaces and ip addresses
78. ip address add 192.168.0.1 dev eth0  : Sets ip address of eth0 device
79. ethtool eth0       : Linux tool to show ethernet status
80. mii-tool  eth0     : Linux tool to show eth0 status
81. ping host          : Sends echo requests to the host to test ipv4 connection
82. whois domain       : Gets who is information for domain
83. dig domain         : Gets DNS nameserver information for domain
    dig -x host        : Reverse lookup host 
84. host google.com    : Lookup DNS ip address for the name
85. hostname -i        : Lookup local ip address
86. wget file          : Downloads file
87. netstat  -tupl     : Lists all active listening ports
88. nslookup           : Resolves domain names to IP addresses

Compression / Archive Related Commands

These commands are used to compress and decompress files
89. tar cf home.tar  home         : Creates a tar named home.tar containing home/
    tar xf file.tar               : Extracts the files from file.tar
    tar czf  file.tar.gz  files   : Creates a tar with gzip compression
90. gzip file                     : Compresses file and renames it to file.gz
91. bzip2 -z file                 : Compresses file and renames it to file.bz2
    bzip2 -d file.bz2             : Decompress the file

Package Installation Related Commands

These commands are used to manage Linux packages
92. rpm -i pkgname.rpm     : Installs rpm based package
    rpm -e pkgname         : Removes package
93. make                   : Install from source file

Search Related Commands

These commands are used to search for files and patterns
94. grep pattern files              : Searches for pattern in files
    grep -r pattern dir             : Searches recursively for pattern in dir
95. locate file                     : Finds all instances of file
96. find /home/tom -name 'index*'   : Finds file names that start with "index" inside /home/tom directory
    find /home -size +10000k        : Finds files larger than 10000k in /home

Login Related Commands

These commands are used to log into another host
97. ssh user@host              : Securely connect to a host as user
    ssh -p port $ user@host    : Connects to host using specific port
98. telnet host                : Connects to the system using  telnet port

File Transfer Related Commands

These commands are used to copy files from one system to another system
99. scp file.txt   server2:/tmp                  : Secure copy file.txt to remote host  /tmp folder
    scp nixsavy@server2:/www/*.html /www/tmp     : Copies *.html files from remote host to current host /www/tmp folder
    scp -r nixsavy@server2:/www   /www/tmp       : Copies all files and folders recursively from remote server to the current system /www/tmp folder
100. rsync -a /home/apps /backup/                 : Synchronizes source to destination
    rsync -avz /home/apps $ linoxide@192.168.10.1:/backup  : Synchronize files/directories between the local and remote system with compression enabled

Disk Usage Related Commands

These commands are used to view disk statistics
101.  df -h                          : Shows free space on mounted filesystems
     df -i                      : Shows free inodes on mounted filesystems
102. fdisk -l                     : Shows disks partitions sizes and types
103. du -ah                         : Displays disk usage in human readable form
     du -sh                         : Displays total disk usage on the current directory
104. findmnt                        : Displays target mount point for all filesystems
105. mount device-path mount-point  : Mounts a device to the device-path

Directory Traverse Related Commands

These commands are used to change the directory
106. cd ..          : Goes up one level of the directory tree
     cd             : Goes to $HOME directory
     cd /test       : Changes to /test directory