DEV Community

TimDKChen
TimDKChen

Posted on

Most common-used Linux command

Part 1: The operaton of file or directory

  1. ls ls command is oen of the most frequently commands in Linux. It can views files permissions, folders, directories, and so on.

1.1 ls -l
Shows file or directory, size, modified date and time, file or folder name and owner of the file, and its permission.
Image description

1.2 ls -a
View hidden files which start with ".".
Image description

ls -lh
With a combination of lh option, shows sizes in a human-readable format.
Image description

1.3 ls -R
List very long listing directory trees.
Image description

1.4 ls -lS
Sort files by file size in linux.
Image description

1.5 ls -l /directory
List files under directory.
Image description

  1. cd

2.1 cd /
Enter root directory of system.

2.2 cd /home/tim802
Jump to "/home/tim802" directory.

  1. pwd
    pwd
    view the path of current working directory

  2. mkdir
    Create an empty directory.

4.1 mkdir test
Create an empty directory.

4.2 mkdir -m 777 test2
Create an empty direcoty with permission 777.

  1. rm Remove files or directories. Without -r, it does not remove the directories.

5.1 rm filename
Remove files.

5.2 rm -f filename
Force to remove file without reminder.

5.3 rm -r directoryname
Delete the directory's subdirectory and all files in the directory.

  1. mv It can be used to move a file or rename a file. When the second argument type is a file, the mv command completes the file renaming. If the second paramenter is an existing directory name, multiple source files or directories can be specified. The mv command moves the specified source files to the destination directory.

6.1 mv file1.txt file2.txt
Rename file1 to file2.

6.2 mv file1.txt test
Move file1.txt to test directory.

6.3 mv file1.txt file2.txt file3.txt test
Move file1.txt file2.txt file3.txt to directory.

  1. cp Copy a source file to a destination file, or copies multiple source files to a destination directory.

7.1 cp file1.txt test
Copy file1.txt to test directory. If the file exists, it will prompt yout whether to overwrite it. Otherwise, replicate directly.

7.2 cp -a test1 test
Copy whole directory test1 into test directory.

  1. touch Touch command can change the date and time of a document or directory, including access time and change time.

8.1 touch newfile.txt
Create a file that does not exist.

8.2 touch -r file1.txt standard.txt
Update the date and time of file1.txt with standard.txt.

  1. cat Display the contents of a file, or to concatenate several files, or to read and display the contents from standard input, often in conjunction with the redirection symbol.

9.1 cat -n file.log > test.log
Add line numbers to the content of the test.log file and type it into the test1.log file.

9.2 tac test.log
Display the contents of the test.log file in reverse order.
Image description

10 head
Display the beginning of the file to standard output. The default head command prints the first 10 lines of the corresponding file.

10.1 head -n 5 test.log
Display the first 5 lines of the test.log file.

10.2 head -c 20 test.log
Display the first 20 bytes of the test.log file.
Image description

11 tail
Display the content at the end of a specified file. If no dile is specified, the file is processed as input information. View log files frequently.

11.1 tail -n 5 test.log
Display the last 5 lines of the test.log file.

11.2 tail -f test.log
Output appended data as the file grows.

Part 2: Search files
12 which
Which command searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result.
Image description

13 whereis
Whereis command locates executables, source code files, and help files in the file system.
Image description
13.2 whereis -b git
Only search for 2-bit files.

14 locate
Find files by name.

14.1 locate pwd
Find all files related to PWD.

14.2 locate /etc/m
Search all files starting with "m" in etc directory.

15 find
Search for files in a directory hierarchy.

15.1 find . -print
Prints the current directory hierarchy.

15.2 find . ! -name "*.txt"
Prints all files excluding suffix with "txt" in current directory.

15.3 find . -type f name "*.php" -perm 777
Prints all 777 permission PHP files in the current directory.

15.4 find . -name "*.php" -exec ls -l {} \;
Find all PHP files in the current directory and display details.

15.5 find . -type f -name "*.c" | xargs wc -l
Find all C code files in the current directory and count the total number of lines.

Part 3: Upload or download files

16 tar
Used to compress and decompress files. Tar itself does not have compression. It is implemented by calling the compression function.

16.1

# Just package the files
tar -cvf test.tar test.log
# Compressed files with gzip
tar -zcvf test.tar.gz test.log
# Compressed files with bzip2
tar -zcvf test.tar.bz2 test.log
# decompress files
tar -zxvf test.tar.gz
Enter fullscreen mode Exit fullscreen mode

17 gzip

# compress
gzip filename
# decompress
gzip -d filename
Enter fullscreen mode Exit fullscreen mode

Part 4: setting the permission of the files

18 chmod
u: The current user of a directories or files
g: The current group of directories or files
o: A user or group oher than the current user or group of a directory of file
a: All users and groups

r: Read permission, represented by the number 4
w: Write permission, represented by the number 2
x: Excute permission, represented by the number 1
-: Delete permission, represented by the digit 0

# Add the execute permission to all user groups for a file
chmod a+x test.log
# Delete the executable permission of all users
chmod a-x test.log
Enter fullscreen mode Exit fullscreen mode

19 chown
Change file owner and group
chown [owner]:[group] test.log

Part 5: Disk Storage
20 df [filename/directory]
Display the available space for files on the specified disk.

21 du
Display the disk usage for each file and directory.
Image description

Part 6: Performance monitoring and tuning commands
22 top
Display the information about processes that are running, including process IDS, memory usage, and CPU usage.

23 free
Displays the used and free memory of the system, including physical memory, interactive area memory(SWAP), and kernel buffer memory.

free
GB as a unit
free -g
Megabyte as unit
free -m
Enter fullscreen mode Exit fullscreen mode

24 vmstat
Used to display virtual memory information.

24.1 vmstat -a 5 5
5 sampling times within 5 seconds
Image description

25 lsof
Display the port number of process opened(TCP, UDP).
Image description

Part 7: Network

26 ipconfig
View and configure network devices.

26.1 launch or close network adapter

ifconfig eth0 up
ifconfig eth0 down
Enter fullscreen mode Exit fullscreen mode

26.2 modified MAC address
ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

27 netstat
Display statistics related to IP, TCp, UDP, and ICMP protocols, and is generally used to check the network connection of each port on the local host.
Image description

Part 8: Others
28 ln
Make links between files. (Soft links and hard links)

# soft link
ln -s test.log link.txt
# hard link
ln test.log link.txt
Enter fullscreen mode Exit fullscreen mode

29 diff
Compare the contents of individual files or directories.
diff file1.txt file2.txt

30 grep
grep [option] pattern file

31 wc
Display lines, words, bytes.
Image description

32 ps
Display the status of current processes.

# show all processes
ps -A
# show specified user
ps -u root
# show all processes and terminals
ps -ef
Enter fullscreen mode Exit fullscreen mode

Top comments (0)