Basic linux command-with detailed sample

2023-05-07,,

Here I will list some parameters which people use very ofen, I will attach the output of the command with one parameters as well.

1.   Create a new user:useradd

Parameter:                                                                                                                                  
- Create new user:                                                              
                                                                                
  useradd name                                                                  
                                                                                
- Create new user with a default home directory:                                
                                                                                
  useradd -m name                                                               
                                                                                
- Create new user with specified shell:                                         
                                                                                
  useradd -s /path/to/shell name                                                
                                                                                
- Create new user with supplementary groups (mind the lack of whitespace):      
                                                                                
  useradd -G group1,group2 name

Sample:

 [root@TestingMachine Desktop]# useradd -s /bin/shell test
[root@TestingMachine Desktop]#

2. Modifies a user account: usermod
Parameter:                                                                            
- Change a user's name:                                                         
                                                                                
  usermod -l newname user                                                       
                                                                                
- Add user to supplementary groups (mind the whitespace):                       
                                                                                
  usermod -a -G group1,group2 user                                              
                                                                                
- Create a new home directory for a user and move their files to it:            
                                                                                
  usermod -m -d /path/to/home user  
Sample:

 [root@TestingMachine Desktop]# usermod -p  test
[root@TestingMachine Desktop]#

3.  Remove a user:userdel

Pamater:                                                                         
- Remove a user and their home directory:                                       
                                                                                
  userdel -r name
Sample:

 [root@TestingMachine Desktop]# userdel -r test
[root@TestingMachine Desktop]#

4.  passwd: Passwd is a tool used to change a user's password.
Parameter:                                                                             
- Change the password of the current user:                                      
                                                                                
  passwd new password                                                           
                                                                                
- Change the password of the specified user:                                    
                                                                                
  passwd username new password                                                  
                                                                                
- Get the current statuts of the user:                                          
                                                                                
  passwd -S                                                                     
                                                                                
- Make the password of the account blank (it will set the named account passwordless):
                                                                                
  passwd -d

Sample:

 [root@TestingMachine Desktop]# passwd test
Changing password for user test.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
[root@TestingMachine Desktop]#

5.  ls:List directory contents.                                                      

Parameter:                                                                       
- List files one per line:                                                      
                                                                                
  ls -1                                                                         
                                                                                
- List all files, including hidden files:                                       
                                                                                
  ls -a                                                                         
                                                                                
- Long format list (permissions, ownership, size and modification date) of all files:
                                                                                
  ls -la                                                                        
                                                                                
- Long format list with size displayed using human readable units (KB, MB, GB):
                                                                                
  ls -lh                                                                        
                                                                                
- Long format list sorted by size (descending):                                 
                                                                                
  ls -lS                                                                        
                                                                                
- Long format list of all files, sorted by modification date (oldest first):    
                                                                                
  ls -ltr

Sample:

 [root@TestingMachine Desktop]# ls -lS
total
-rw-r--r--. root root May : CentOS-6.7-x86_64-bin-DVD1.iso
-rwxr-xr-x. root root May : gnome-terminal.desktop
drwxr-xr-x. root root May : Python_file
-rw-r--r--. root root May : linux_command
-rw-r--r--. root root May : test

6. cp:Copy files.                                                                   
Parameter:                                                                         
- Copy files in arbitrary locations:                                            
                                                                                
  cp /path/to/original /path/to/copy                                            
                                                                                
- Copy a file to a parent directory:                                            
                                                                                
  cp /path/to/original ../path/to/copy                                          
                                                                                
- Copy directories recursive using the option -r:                               
                                                                                
  cp -r /path/to/original /path/to/copy                                         
                                                                                
- Show files as they are copied:                                                
                                                                                
  cp -vr /path/to/original /path/to/copy                                        
                                                                                
- Make a copy of a file, adding an extension:                                   
                                                                                
  cp file.html{,.backup}                                                        
                                                                                
- Make a copy of a file, changing the extension:                                
                                                                                
  cp file.{html,backup}

Sample:

 [root@TestingMachine Desktop]# ls -al test*
-rw-r--r--. root root May : test.txt
[root@TestingMachine Desktop]# cp test.{txt,html}
[root@TestingMachine Desktop]# ls -al test*
-rw-r--r--. root root May : test.html
-rw-r--r--. root root May : test.txt

7. mv:Move or rename files and directories.                                         
Parameter:                                                                            
- Move files in arbitrary locations:                                            
                                                                                
  mv source target                                                              
                                                                                
- Do not prompt for confirmation before overwriting existing files:             
                                                                                
  mv -f source target                                                           
                                                                                
- Do not prompt for confirmation before overwriting existing files but write to standard error before overriding:
                                                                                
  mv -fi source target                                                          
                                                                                
- Move files in verbose mode, showing files after they are moved:               
                                                                                
  mv -v source target

Sample:

 [root@TestingMachine Desktop]# mv -v test.html /home/test
`test.html' -> `/home/test/test.html'

8. rm:Remove files or directories.                                                  
Sample:                                                                         
- Remove files from arbitrary locations:                                        
                                                                                
  rm /path/to/file /otherpath/to/file2                                          
                                                                                
- Remove recursively a directory and all its subdirectories:                    
                                                                                
  rm -r /path/to/folder                                                         
                                                                                
- Remove directory without prompt:                                              
                                                                                
  rm -rf /path/to/folder                                                        
                                                                                
- Prompt before every removal:                                                  
                                                                                
  rm -i \*

Sample:

[root@TestingMachine test]# ls -al
total
drwxr-xr-x. root root May : .
drwxr-xr-x. root root May : ..
drwxr-xr-x. root root May : test
[root@TestingMachine test]# rm -rf test
[root@TestingMachine test]# ls -al
total
drwxr-xr-x. root root May : .
drwxr-xr-x. root root May : ..

9. mkdir: Creates a directory.                                                          

Parameter:                                                                          
- Create a directory in current folder or given path:                           
                                                                                
  mkdir directory                                                               
                                                                                
- Create directories recursively (useful for creating nested dirs):             
                                                                                
  mkdir -p path

Sample:

 [root@TestingMachine Desktop]# mkdir -p test/test1/test2/test3
[root@TestingMachine Desktop]# ls -al test/test1/test2/test3
total
drwxr-xr-x. root root May : .
drwxr-xr-x. root root May : ..

10.  rmdir:Removes a directory.                                                          
Parameter:                                                                        
- Remove directory, provided it is empty. Use `rm` to remove not empty directories:
                                                                                
  rmdir directory                                                               
                                                                                
- Remove directories recursively (useful for nested dirs):                      
                                                                                
  rmdir -p path

Sample:

 [root@TestingMachine Desktop]# ls -al test/test1/test2/test3
total
drwxr-xr-x. root root May : .
drwxr-xr-x. root root May : ..
[root@TestingMachine Desktop]# rmdir -p test/test1/test2/test3
[root@TestingMachine Desktop]# ls -al
total
drwxr-xr-x. root root May : .
dr-xr-x---. root root May : ..
-rw-r--r--. root root May : CentOS-6.7-x86_64-bin-DVD1.iso
-rwxr-xr-x. root root May : gnome-terminal.desktop
-rw-r--r--. root root May : linux_command
drwxr-xr-x. root root May : Python_file
-rw-r--r--. root root May : test.txt

11. cd:Change the current working directory.                                         
Parameter:                                                                             
- Go to the given directory:                                                    
                                                                                
  cd /path/to/directory                                                         
                                                                                
- Go to home directory of current user:                                         
                                                                                
  cd                                                                            
                                                                                
- Go up to the parent of the current directory:                                 
                                                                                
  cd ..                                                                         
                                                                                
- Go to the previously chosen directory:                                        
                                                                                
  cd -

Sample:

 [root@TestingMachine Desktop]# cd
[root@TestingMachine ~]#

12. chmod:Change the access permissions of a file or directory.                         
Parameter:                                                                               
- Give the (u)ser who owns a file the right to e(x)ecute it:                    
                                                                                
  chmod u+x file                                                                
                                                                                
- Give the user rights to (r)ead and (w)rite to a file/directory:               
                                                                                
  chmod u+rw file                                                               
                                                                                
- Remove executable rights from the (g)roup:                                    
                                                                                
  chmod g-x file                                                                
                                                                                
- Give (a)ll users rights to read and execute:                                  
                                                                                
  chmod a+rx file                                                               
                                                                                
- Give (o)thers (not in the file owner's group) the same rights as the group:   
                                                                                
  chmod o=g file

Sample:

[root@TestingMachine Desktop]# ls -al test.txt
-rw-r--r--. root root May : test.txt
[root@TestingMachine Desktop]# chmod a+rx test.txt
[root@TestingMachine Desktop]# ls -al test.txt
-rwxr-xr-x. root root May : test.txt

13. chown:Change user and group ownership of files and folders.                         
Parameter:                                                                            
- Change the owner user of a file/folder:                                       
                                                                                
  chown user path/to/file                                                       
                                                                                
- Change the owner user and group of a file/folder:                             
                                                                                
  chown user:group path/to/file                                                 
                                                                                
- Recursively change the owner of a folder and its contents:                    
                                                                                
  chown -R user path/to/folder                                                  
                                                                                
- Change the owner of a symbolic link:                                          
                                                                                
  chown -h user path/to/symlink                                                 
                                                                                
- Change the owner of a file/folder to match a reference file:                  
                                                                                
  chown --reference=path/to/reference_file path/to/file

Sample:

[root@TestingMachine Desktop]# ls -al test.txt
-rwxr-xr-x. root root May : test.txt
[root@TestingMachine Desktop]# chown -R test test.txt
[root@TestingMachine Desktop]# ls -al test.txt
-rwxr-xr-x. test root May : test.txt

14. find:Find files under the given directory tree, recursively.                       
Parameter:                                                                               
- Find files by extension:                                                      
                                                                                
  find root_path -name '*.py'                                                   
                                                                                
- Find files matching path pattern:                                             
                                                                                
  find root_path -path '**/lib/**/*.py'                                         
                                                                                
- Run a command for each file, use {} within the command to access the filename:
                                                                                
  find root_path -name '*.py' -exec wc -l {} \;                                 
                                                                                
- Find files modified since a certain time:                                     
                                                                                
  find root_path -name '*.py' -mtime -1d                                        
                                                                                
- Find files using case insensitive name matching, of a certain size:           
                                                                                
  find root_path -size +500k -size -10MB -iname '*.TaR.gZ'                      
                                                                                
- Delete files by name, older than a certain number of days:                    
                                                                                
  find root_path -name '*.py' -mtime -180d -delete                              
                                                                                
- Find empty files or directories:                                              
                                                                                
  find root_path -empty                                                         
                                                                                
- Find files matching more than one search criteria:                            
                                                                                
  find root_path -name '*.py' -or -name '*.r'

Sample:

 [root@TestingMachine Desktop]# find /root -name '*.txt' -mtime - | more
/root/Desktop/test.txt
/root/Downloads/pycharm-community-2016.1./plugins/svn4idea/lib/licenses/LICENSE
-JAVAHL.txt
/root/Downloads/pycharm-community-2016.1./plugins/svn4idea/lib/licenses/SQLJET-
README.txt

15.  date:Set or display the system date.                                               
Parameter:                                                                            
- Display the date using the default locale:                                    
                                                                                
  date +"%c"                                                                    
                                                                                
- Display the date in UTC and ISO 8601 format:                                  
                                                                                
  date -u +"%Y-%m-%dT%H:%M:%SZ"

Sample:

 [root@TestingMachine Desktop]# date -u +"%Y-%m-%dT%H:%M:%SZ"
--14T07::45Z
[root@TestingMachine Desktop]#

16. reboot: Reboot the system.                                                            
Parameter:                                                                               
- Reboot immediately:                                                           
                                                                                
  reboot                                                                        
                                                                                
- Reboot immediately without gracefully shutdown:                               
                                                                                
  reboot -f

Sample:

 [root@TestingMachine Desktop]# reboot -r

17. shutdown:Shutdown and reboot the system.                                               
Parameter:                                                                               
- Power off (halt) immediately:                                                 
                                                                                
  shutdown -h now                                                               
                                                                                
- Reboot immediately:                                                           
                                                                                
  shutdown -r now                                                               
                                                                                
- Reboot in 5 minutes:                                                          
                                                                                
  shutdown -r +5 &                                                              
                                                                                
- Shutdown at 1:00 pm (Uses 24h clock):                                                                                                           
  shutdown -h 13:00                                                             
                                                                                
- Cancel a pending shutdown/reboot operation:                                   
                                                                                
  shutdown -c

Sample:

 [root@TestingMachine Desktop]# shutdown -h now

18. route:Use route cmd to set the route table .                                        
Paramter:                                                                               
- Display the information of route table:                                       
                                                                                
  route -n                                                                      
                                                                                
- Add route rule:                                                               
                                                                                
  sudo route add -net ip_address netmask netmask_address gw gw_address          
                                                                                
- Delete route rule:                                                            
                                                                                
  sudo route del -net ip_address netmask netmask_address dev gw_address

Sample:

 [root@TestingMachine Desktop]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.8.0 0.0.0.0 255.255.252.0 U wlan0
0.0.0.0 10.0.8.1 0.0.0.0 UG wlan0

19.  Ifconfig: Interface Configurator, used to configure network interfaces.      
Parameter:                                                                               
- View network settings of an ethernet adapter:                                 
                                                                                
  ifconfig eth0                                                                 
                                                                                
- Display details of all interfaces, including disabled interfaces:             
                                                                                
  ifconfig -a                                                                   
                                                                                
- Disable eth0 interface:                                                       
                                                                                
  ifconfig eth0 down                                                            
                                                                                
- Enable eth0 interface:                                                        
                                                                                
  ifconfig eth0 up                                                              
                                                                                
- Assign IP address to eth0 interface:                                          
                                                                                
  ifconfig eth0 ip_address

Sample:

[root@TestingMachine Desktop]# ifconfig -a
em1 Link encap:Ethernet HWaddr F8:CA:B8::EA:1B
UP BROADCAST MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b)
Interrupt: Memory:f7100000-f7120000

20. hostname:Show or set the system's host name.                                           
Parameter:                                                                               
- Show current host name:                                                       
                                                                                
  hostname                                                                      
                                                                                
- Show the network address of the host name:                                    
                                                                                
  hostname -i                                                                   
                                                                                
- Show all network addresses of the host:                                       
                                                                                
  hostname -I                                                                   
                                                                                
- Show the FQDN (Fully Qualified Domain Name):                                  
                                                                                
  hostname --fqdn                                                               
                                                                                
- Set current host name:                                                        
                                                                                
  hostname new_hostname
Sample:

[root@TestingMachine Desktop]# hostname -I
10.0.9.234

21. last:View the last logged in users.                                                
Parameter:                                                                               
- View last logins, their duration  and other information as read from /var/log/wtmp:
                                                                                
  last                                                                          
                                                                                
- Specify how many of the last logins to show:                                  
                                                                                
  last -n login_count                                                           
                                                                                
- View full login times and dates:                                              
                                                                                
  last -F                                                                       
                                                                                
- View the last login by a specific user:                                       
                                                                                
  last user_name                                                                
                                                                                
- View the last reboot (last login of the pseudo user reboot):                  
                                                                                
  last reboot                                                                   
                                                                                
- View the last shutdown (last login of the pseudo user shutdown):              
                                                                                
  last shutdown

Sample:

[root@TestingMachine Desktop]# last shutdown

wtmp begins Tue May  :: 

22. w:Show who is logged on and what they are doing. Print user login, TTY, remote host, login time, idle time, current process.   
Parameter:                                                                               
- Show logged-in users info:                                                    
                                                                                
  w                                                                             
                                                                                
- Show logged-in users info without a header:                                   
                                                                                
  w -h

Sample:

 [root@TestingMachine Desktop]# w -h
root tty1 : : :05m : : /usr/bin/Xorg :
root pts/ :0.0 : : .07s .07s bash
root pts/ :0.0 : .00s .19s .00s w -h
[root@TestingMachine Desktop]#

23. watch:Execute a program periodically, showing output fullscreen.                    
Parameter:                                                                               
- Repeatedly run a command and show the result:                                 
                                                                                
  watch command                                                                 
                                                                                
- Re-run a command every 60 seconds:                                            
                                                                                
  watch -n 60 command                                                           
                                                                                
- Monitor the contents of a directory, highlighting differences as they appear:
                                                                                
  watch -d ls -l

Sample:

 [root@TestingMachine Desktop]#  watch -d ls -l
[root@TestingMachine Desktop]#

24. pgrep:Find or signal process by name.                                               
 Parameter:                                                                              
- Return PIDs of any running processes with a matching command string:          
                                                                                
  pgrep process_name                                                            
                                                                                
- Search full command line with parameters instead of just the process name:    
                                                                                
  pgrep -f "process_name parameter"                                             
                                                                                
- Search for process run by a specific user:                                    
                                                                                
  pgrep -u root process_name

Sample:

 [root@TestingMachine Desktop]# pgrep -u root ssh
 

25. nohup:Allows for a process to live when the terminal gets killed.                   
Parameter:                                                                               
- Run process that can live beyond the terminal:                                
                                                                                
  nohup command options

Sample:

 [root@TestingMachine Desktop]# nohup ls -al > myout >&
[root@TestingMachine Desktop]# ls -al myout
-rw-r--r--. root root May : myout

26.  ipcs:Display information about ressources used in IPC (Inter-process Communication).
Parameter:                                                                               
- Specific information about the Message Queue which has the id 32768:          
                                                                                
  ipcs -qi 32768                                                                
                                                                                
- General information about all the IPC:                                        
                                                                                
  ipcs -a

Sample:

[root@TestingMachine Desktop]# ipcs -a

------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest
0x00000000 root dest ------ Semaphore Arrays --------
key semid owner perms nsems
0x00000000 root
0x00000000 root ------ Message Queues --------
key msqid owner perms used-bytes messages

27. ps:Information about running processes.                                          
Parameter:                                                                               
- List all running processes:                                                   
                                                                                
  ps aux                                                                        
                                                                                
- List all running processes including the full command string:                 
                                                                                
  ps auxww                                                                      
                                                                                
- Search for a process that matches a string:                                   
                                                                                
  ps aux | grep string

Sample:

 [root@TestingMachine Desktop]# ps aux|grep update
root 0.0 0.0 ? S : : gpk-update-icon
root 0.0 0.0 pts/ S+ : : grep update

28. crontab:Schedule cron jobs to run on a time interval for the current user.            
Parameter:                                                                               
- Edit the crontab file for the current user:                                   
                                                                                
  crontab -e                                                                    
                                                                                
- View a list of existing cron jobs for current user:                           
                                                                                
  crontab -l                                                                    
                                                                                
- Remove all cron jobs for the current user:                                    
                                                                                
  crontab -r

Sample:

 [root@TestingMachine Desktop]# crontab -l
no crontab for root

29. ping:Send ICMP ECHO_REQUEST packets to network hosts.                              
Parameter:                                                                               
- Ping host:                                                                    
                                                                                
  ping host                                                                     
                                                                                
- Ping a host only a specific number of times:                                  
                                                                                
  ping -c count host                                                            
                                                                                
- Ping host, specifying the interval in seconds between requests (default is 1 second):
                                                                                
  ping -i seconds host                                                          
                                                                                
- Ping host without trying to lookup symbolic names for addresses:              
                                                                                
  ping -n host

Sample:

 [root@TestingMachine Desktop]# ping -c  10.0.9.234
PING 10.0.9.234 (10.0.9.234) () bytes of data.
bytes from 10.0.9.234: icmp_seq= ttl= time=0.063 ms
bytes from 10.0.9.234: icmp_seq= ttl= time=0.050 ms
bytes from 10.0.9.234: icmp_seq= ttl= time=0.050 ms
bytes from 10.0.9.234: icmp_seq= ttl= time=0.021 ms --- 10.0.9.234 ping statistics ---
packets transmitted, received, % packet loss, time 2999ms
rtt min/avg/max/mdev = 0.021/0.046/0.063/0.015 ms

30. traceroute:Print the route packets trace to network host.                                
Parameter:                                                                               
- Traceroute to a host:                                                         
                                                                                
  traceroute host                                                               
                                                                                
- Disable IP address and host name mapping:                                     
                                                                                
  traceroute -n host                                                            
                                                                                
- Specify wait time for response:                                               
                                                                                
  traceroute -w 0.5 host                                                        
                                                                                
- Specify number of queries per hop:                                            
                                                                                
  traceroute -q 5 host

Sample:

[root@TestingMachine Desktop]# traceroute www.baidu.com
traceroute to www.baidu.com (61.135.169.121), hops max, byte packets
10.0.8.1 (10.0.8.1) 7.095 ms 7.105 ms 7.105 ms
172.19.141.177 (172.19.141.177) 10.667 ms 10.673 ms 10.677 ms
172.16.0.81 (172.16.0.81) 10.617 ms 11.419 ms 11.437 ms
* * *
61.148.50.113 (61.148.50.113) 35.461 ms 35.490 ms 35.491 ms
61.148.6.193 (61.148.6.193) 19.384 ms bt--.bta.net.cn (202.106.229.117) 6.069 ms 61.148.154.249 (61.148.154.249) 6.032 ms
61.148.156.177 (61.148.156.177) 10.794 ms 8.855 ms 8.797 ms
123.126.6.118 (123.126.6.118) 8.710 ms 12.046 ms 24.462 ms
61.49.168.86 (61.49.168.86) 55.927 ms 61.49.168.82 (61.49.168.82) 35.308 ms 61.49.168.86 (61.49.168.86) 55.922 ms

31. netstat:Displays various networks related information such as open connections, open socket ports etc.
Parameter:                                                                               
- List all ports:                                                               
                                                                                
  netstat -a                                                                    
                                                                                
- List all listening ports:                                                     
                                                                                
  netstat -l                                                                    
                                                                                
- List listening TCP ports:                                                     
                                                                                
  netstat -t                                                                    
                                                                                
- Display PID and program names:                                                
                                                                                
  netstat -p                                                                    
                                                                                
- List information continuously:                                                
                                                                                
  netstat -c                                                                    
                                                                                
- List routes and do not resolve IP to hostname:                                
                                                                                
  netstat -rn                                                                   
                                                                                
- List listening TCP and UDP ports (+ user and process if you're root):         
                                                                                
  netstat -lepunt

Sample:

[root@TestingMachine Desktop]# netstat -t
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 10.0.9.234: a23---.deploy.:http CLOSE_WAIT

32. mount:Provides access to an entire filesystem in one directory.                     
Parameter:                                                                               
- Show all mounted filesystems:                                                 
                                                                                
  mount                                                                         
                                                                                
- Mount a device:                                                               
                                                                                
  mount -t filesystem_type path_to_device_file directory_to_mount_to            
                                                                                
- Mount a CD-ROM device (with the filetype ISO9660) to /cdrom (readonly):       
                                                                                
  mount -t iso9660 -o ro /dev/cdrom /cdrom                                      
                                                                                
- Mount all the filesystem defined in /etc/fstab:                               
                                                                                
  mount -a                                                                      
                                                                                
- Mount a specific filesystem described in /etc/fstab (e.g. "/dev/sda1 /my_drive ext2 defaults 0 2"):
                                                                                
  mount /my_drive

Sample:

[root@TestingMachine Desktop]# mount CentOS-6.7-x86_64-bin-DVD1.iso /mnt/iso -o loop
[root@TestingMachine Desktop]#

33. mke2fs:Creates a Linux filesystem inside a partition.                                
Parameter:                                                                              
- Create an ext2 filesystem in partition 1 of device b (`sdb1`):                
                                                                                
  mkfs.ext2 /dev/sdb1                                                           
                                                                                
- Create an ext3 filesystem in partition 1 of device b (`sdb1`):                
                                                                                
  mkfs.ext3 /dev/sdb1

Sample:

[root@TestingMachine Desktop]# mke2fs -q /dev/sda1
/dev/sda1 is mounted; will not make a filesystem here!

34. which:Locate the a program in the user's path.                                      
Parameter:                                                                               
- Search the PATH environment variable and display the location of any matching executables:
                                                                                
  which executable                                                              
                                                                                
- If there are multiple executables which match, display all:                   
                                                                                
  which -a executable

Sample:

[root@TestingMachine Desktop]# which python
/usr/local/bin/python

35. who:Display who is logged in and related data (processes, boot time).             
Parameter:                                                                              
- Display the username, line, and time of all currently logged-in sessions:     
                                                                                
  who                                                                           
                                                                                
- Display information only for the current terminal session:                    
                                                                                
  who am i                                                                      
                                                                                
- Display all available information:                                            
                                                                                
  who -a                                                                        
                                                                                
- Display all available information with table headers:                         
                                                                                
  who -a -H

Sample:

[root@TestingMachine Desktop]# who -a -H
NAME LINE TIME IDLE PID COMMENT EXIT
system boot -- :
run-level -- :
LOGIN tty3 -- : id=
LOGIN tty2 -- : id=
LOGIN tty4 -- : id=
LOGIN tty5 -- : id=
LOGIN tty6 -- : id=
root + tty1 -- : old (:)
root + pts/ -- : . (:0.0)
root + pts/ -- : . (:0.0)

36. touch:Change a file access and modification times (atime, mtime).                   
Parameter:                                                                               
- Create a new empty file(s) or change the times for existing file(s) to current time:
                                                                                
  touch filename                                                                
                                                                                
- Set the times on a file to a specific date and time:                          
                                                                                
  touch -t YYYYMMDDHHMM.SS filename                                             
                                                                                
- Use the times from a file to set the times on a second file:                  
                                                                                
  touch -r filename filename2

Sample:

[root@TestingMachine Desktop]# touch -r test.txt test
[root@TestingMachine Desktop]# ls -al tes*
-rw-r--r--. root root May : test
-rwxr-xr-x. test root May : test.txt

37. head:Output the first part of files.                                               
Parameter:                                                                               
- Output the first few lines of a file:                                         
                                                                                
  head -n count_of_lines filename                                               
                                                                                
- Output the first few bytes of a file:                                         
                                                                                
  head -c size_in_bytes filename

Sample:

[root@TestingMachine Desktop]# head -n  myout
nohup: ignoring input
total
drwxr-xr-x. root root May : .
dr-xr-x---. root root May : ..
-rw-r--r--. root root May : CentOS-6.7-x86_64-bin-DVD1.iso

38. tail:Display the last part of a file.                                              
Parameter:                                                                               
- Show last 'num' lines in file:                                                
                                                                                
  tail -n num file                                                              
                                                                                
- Show all file since line 'num':                                               
                                                                                
  tail -n +num file                                                             
                                                                                
- Show last 'num' bytes in file:                                                
                                                                                
  tail -c num file                                                              
                                                                                
- Keep reading file until ctrl-c:                                               
                                                                                
  tail -f file

Sample:

[root@TestingMachine Desktop]# tail -n  myout
-rwxr-xr-x. root root May : gnome-terminal.desktop
-rw-r--r--. root root May : linux_command
-rw-r--r--. root root May : myout
drwxr-xr-x. root root May : Python_file
-rwxr-xr-x. test root May : test.txt

39. diff: Compare files and directories.                                                
 Parameter:                                                                              
- Compare files:                                                                
                                                                                
  diff file1 file2                                                              
                                                                                
- Compare files, ignoring white spaces:                                         
                                                                                
  diff -w file1 file2                                                           
                                                                                
- Compare files, showing differences side by side:                              
                                                                                
  diff -y file1 file2                                                           
                                                                                
- Compare directories recursively:                                              
                                                                                
  diff -r directory1 directory2                                                 
                                                                                
- Compare directories, only showing the names of files that differ:             
                                                                                
  diff -rq directory1 directory2

Sample:

 [root@TestingMachine Desktop]# diff -w myout myout1
2c2
< total
---
> total
4a5
> -rw-r--r--. root root May : CentOS-6.7-x86_64-bin-DVD1.iso

40. sort:Sort lines of text files.                                                     
Parameter:                                                                               
- Sort a file in ascending order:                                               
                                                                                
  sort filename                                                                 
                                                                                
- Sort a file in descending order:                                              
                                                                                
  sort -r filename                                                              
                                                                                
- Sort a file using numeric rather than alphabetic order:                       
                                                                                
  sort -n filename                                                              
                                                                                
- Sort the passwd file by the 3rd field, numerically:                           
                                                                                
  sort -t: -k 3n /etc/passwd                                                    
                                                                                
- Sort a file preserving only unique lines:                                     
                                                                                
  sort -u filename

Sample:

[root@TestingMachine Desktop]# sort -t ":" -k 3n /etc/passwd
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
adm:x:::adm:/var/adm:/sbin/nologin
lp:x:::lp:/var/spool/lpd:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync

41. wc:Count words, bytes, or lines.                                                 
 Parameter:                                                                              
- Count lines in file:                                                          
                                                                                
  wc -l file                                                                    
                                                                                
- Count words in file:                                                          
                                                                                
  wc -w file                                                                    
                                                                                
- Count characters (bytes) in file:                                             
                                                                                
  wc -c file                                                                    
                                                                                
- Count characters in file (taking multi-byte character sets into account):     
                                                                                
  wc -m file

Sample:

[root@TestingMachine Desktop]# wc -l /etc/passwd
/etc/passwd

42. locate:Find filenames quickly.                                                       
Parameter:                                                                               
- Look for pattern in the database. Note: the database is recomputed periodically (usually weekly or daily):
                                                                                
  locate pattern                                                                
                                                                                
- Recompute the database. You need to do it if you want to find recently added files:
                                                                                
  sudo updatedb

Sample:

[root@TestingMachine Desktop]# locate test.txt
/root/Downloads/Python-2.7./Lib/test/test_doctest.txt
/root/Downloads/Python-3.5./Lib/test/test_doctest.txt
/usr/local/lib/python2./test/test_doctest.txt
/usr/local/lib/python3./test/test_doctest.txt

43. gzip:Compress/uncompress files with gzip compression (LZ77).                       
Parameter:                                                                               
- Compress a file, replacing it with a gzipped compressed version:              
                                                                                
  gzip file.ext                                                                 
                                                                                
- Decompress a file, replacing it with the original uncompressed version:       
                                                                                
  gzip -d file.ext.gz                                                           
                                                                                
- Compress a file specifying the output filename:                               
                                                                                
  gzip -c file.ext > compressed-file.ext.gz                                     
                                                                                
- Uncompress a gzipped file specifying the output filename:                     
                                                                                
  gzip -c -d file.ext.gz > uncompressed-file.ext                                
                                                                                
- Specify the compression level. 1=Fastest (Worst), 9=Slowest (Best), Default level is 6:
                                                                                
  gzip -9 -c file.ext > compressed-file.ext.gz

Sample:

[root@TestingMachine Desktop]# gzip - -c test.txt > compressed-file.ext.gz
[root@TestingMachine Desktop]# ls
CentOS-6.7-x86_64-bin-DVD1.iso linux_command Python_file
compressed-file.ext.gz myout test

44. tar:Archiving utility. Optional compression with gzip / bzip.                                        
Parameter:                                                                               
- Create an archive from files:                                                 
                                                                                
  tar cf target.tar file1 file2 file3                                           
                                                                                
- Create a gzipped archive:                                                     
                                                                                
  tar czf target.tar.gz file1 file2 file3                                       
                                                                                
- Extract an archive in a target folder:                                        
                                                                                
  tar xf source.tar -C folder                                                   
                                                                                
- Extract a gzipped archive in the current directory:                           
                                                                                
  tar xzf source.tar.gz                                                         
                                                                                
- Extract a bzipped archive in the current directory:                           
                                                                                
  tar xjf source.tar.bz2                                                        
                                                                                
- Create a compressed archive, using archive suffix to determine the compression program:
                                                                                
  tar caf target.tar.xz file1 file2 file3                                       
                                                                                
- List the contents of a tar file:                                              
                                                                                
  tar tvf source.tar

Sample:

[root@TestingMachine Desktop]# tar cvf test.tar test.txt
test.txt

45. df:Gives an overview of the file system disk space usage.                        
Parameter:                                                                               
- Display all file systems and their disk usage:                                
                                                                                
  df                                                                            
                                                                                
- Display all file systems and their disk usage in human readable form:         
                                                                                
  df -h

Sample:

[root@TestingMachine Desktop]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 289G .6G 266G % /
tmpfs 16G 228K 16G % /dev/shm
/dev/sda1 .6G 58M .2G % /boot
/root/Desktop/CentOS-6.7-x86_64-bin-DVD1.iso
.7G .7G % /mnt/iso

46 .top:Display dynamic real-time information about running processes.                
 Parameter:                                                                              
- Start top:                                                                    
                                                                                
  top                                                                           
                                                                                
- Start top ignoring any idle or zombie processes:                              
                                                                                
  top -i                                                                        
                                                                                
- Start top displaying only processes owned by given user:                      
                                                                                
  top -u user-name                                                              
                                                                                
- Get help about interactive commands:                                          
                                                                                
  ?

Sample:

[root@TestingMachine Desktop]# top -u test

top - :: up  :,   users,  load average: 0.02, 0.02, 0.00
Tasks: total, running, sleeping, stopped, zombie
Cpu(s): 0.8%us, 0.3%sy, 0.0%ni, 99.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Mem: 32426484k total, 1784640k used, 30641844k free, 101120k buffers
Swap: 8191996k total, 0k used, 8191996k free, 533040k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND

47. kill:Sends a signal to a process. Mostly used for stopping processes.                                           
Parameter:                                                                               
- Kill the process:                                                             
                                                                                
  kill process_id                                                               
                                                                                
- List signal names:                                                            
                                                                                
  kill -l

Sample:

[root@TestingMachine Desktop]# kill -l
) SIGHUP ) SIGINT ) SIGQUIT ) SIGILL ) SIGTRAP
) SIGABRT ) SIGBUS ) SIGFPE ) SIGKILL ) SIGUSR1
) SIGSEGV ) SIGUSR2 ) SIGPIPE ) SIGALRM ) SIGTERM
) SIGSTKFLT ) SIGCHLD ) SIGCONT ) SIGSTOP ) SIGTSTP
) SIGTTIN ) SIGTTOU ) SIGURG ) SIGXCPU ) SIGXFSZ
) SIGVTALRM ) SIGPROF ) SIGWINCH ) SIGIO ) SIGPWR
) SIGSYS ) SIGRTMIN ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMIN+
) SIGRTMIN+ ) SIGRTMIN+ ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX- ) SIGRTMAX-
) SIGRTMAX- ) SIGRTMAX

48. telnet:Telnet is used to connect to a specified port of a host.                      
Parameter:                                                                               
- Telnet to a certain port:                                                     
                                                                                
  telnet  ip_address port                                                       
                                                                                
- To exit a telnet session:                                                     
                                                                                
  quit                                                                          
                                                                                
- Default escape character:                                                     
                                                                                
  CTRL + ]                                                                      
                                                                                
- Specify an escape character (x is the escape character):                      
                                                                                
  telnet -e x ip_address port

Sample:

[root@TestingMachine Desktop]# telnet 10.208.131.30 

49. man:Format and display manual pages.                                              
Parameter:                                                                               
- Display man page for a command:                                               
                                                                                
  man command                                                                   
                                                                                
- Display path searched for manpages:                                           
                                                                                
  man --path                                                                    
                                                                                
- Display location of a manpage rather than the manpage itself:                 
                                                                                
  man -w command                                                                
                                                                                
- Do a keyword search for manpages containing a search string:                  
                                                                                
  man -k keyword

Sample:

[root@TestingMachine Desktop]# man ssh

50. free:Display amount of free and used memory in the system.                         
Parameter:                                                                               
- Display system memory:                                                        
                                                                                
  free                                                                          
                                                                                
- Display memory in Bytes/KB/MB/GB:                                             
                                                                                
  free -b/-k/-m/-g                                                              
                                                                                
- Display memory in human readable units:                                       
                                                                                
  free -h                                                                       
                                                                                
- Continuous monitor memory (refresh every X seconds):                          
                                                                                
  free -s X

 Sample:

[root@TestingMachine Desktop]# free -h
total used free shared buffers cached
Mem: 30G .7G 29G 4.5M 99M 521M
-/+ buffers/cache: .1G 29G
Swap: .8G 0B .8G

Basic linux command-with detailed sample的相关教程结束。

《Basic linux command-with detailed sample.doc》

下载本文的Word格式文档,以方便收藏与打印。