samwellwang

samwellwang

coder
twitter

Common Linux Commands

Common Linux Commands#

This article introduces some commonly used Linux commands in daily work, applicable to Ubuntu (I will make annotations if there are differences on CentOS).

1. ls#

Description: Display files and directories in the current directory.

Usage: ls [options] [path]

Example: ls -l /home (display detailed information of files and directories in the /home directory)

2. cd#

Description: Change the current working directory.

Usage: cd [directory]

Example: cd /var/www (switch to the /var/www directory)

3. pwd#

Description: Display the path of the current working directory.

Usage: pwd

Example: /home/user/documents

4. mkdir#

Description: Create a new directory.

Usage: mkdir [options] directory

Example: mkdir new_directory (create a new directory named "new_directory" in the current directory)

5. rm#

Description: Delete a file or directory.

Usage: rm [options] file/directory

Example: rm file.txt (delete a file named "file.txt")

6. cp#

Description: Copy a file or directory.

Usage: cp [options] source_path destination_path

Example: cp file.txt /tmp (copy a file named "file.txt" to the /tmp directory)

7. mv#

Description: Move or rename a file or directory.

Usage: mv [options] source_path destination_path

Example: mv file.txt new_location/file_renamed.txt (move the file "file.txt" to the "new_location" directory and rename it as "file_renamed.txt")

8. touch#

Description: Create an empty file or modify the file timestamp.

Usage: touch [options] file

Example: touch new_file.txt (create a new file named "new_file.txt" in the current directory)

9. cat#

Description: Display the contents of a file.

Usage: cat [options] file

Example: cat file.txt (display the contents of the "file.txt" file)

10. grep#

Description: Search for matching text in a file.

Usage: grep [options] pattern file

Example: grep "keyword" file.txt (search for lines containing "keyword" in the "file.txt" file)

11. Count the number of files#

Description: Count the number of files.

Usage: ls -l | grep -c "^-"

Example: ls -l | grep -c "^-" (count the number of files in the current directory)

12. Count the number of directories#

Description: Count the number of directories.

Usage: ls -l | grep "^d" | wc -l

Example: ls -l | grep "^d" | wc -l (count the number of directories in the current directory)

15. Count the number of files in a tar package#

Description: Count the number of files in a tar package.

Usage: tar -tzf file.tar.gz | wc -l

Example: tar -tzf file.tar.gz | wc -l (count the number of files in the tar package named "file.tar.gz")

16. Change the owner and group of a file or directory#

Description: Change the owner and group of a file or directory.

Usage: chown [-R] account_name:group_name file_or_directory

Example: chown -R user:group file.txt (recursively change the owner of the file "file.txt" to "user" and the group to "group")

17. File synchronization (using rsync)#

Description: Synchronize files using the rsync command.

Usage: rsync -vvvrtopg --progress -e 'ssh -p 40012' source_file destination_file

Example: rsync -vvvrtopg --progress -e 'ssh -p 40012' [email protected]:/mnt/downExtra/20220531/vk_image_download/temp/0-10000 ./imageTemp/ (synchronize files from a specified path on a remote server to a local directory)

18. Calculate the md5 value of a file#

Description: Calculate the md5 value of a file.

Usage: md5sum file

Example: md5sum test.txt (calculate the md5 value of the "test.txt" file)

19. Generate an md5 checksum file for a file#

Description: Generate a checksum file containing the md5 values of files.

Usage: find ./myfile/* -type f -print0 | xargs -0 md5sum > md5.txt

Example: find ./myfile/* -type f -print0 | xargs -0 md5sum > md5.txt (generate a checksum file named "md5.txt" containing the md5 values of files in the "./myfile" directory)

20. Verify the md5 checksum of a file#

Description: Verify the md5 checksum of a file.

Usage: md5sum -c md5.txt > md5.check

Example: md5sum -c md5.txt > md5.check (verify the md5 checksum of a file based on the md5 values in the "md5.txt" file)

21. Stop and start WSL (Windows)#

Description: Stop and start Windows Subsystem for Linux (WSL).

Usage:

  • Stop: wsl --shutdown or net stop LxssManager
  • Start: net start LxssManager

22. Restart xrdp service#

Description: Restart the xrdp service.

Usage: sudo service xrdp restart

Example: sudo service xrdp restart (restart the xrdp service)

23. Start Xfce desktop environment#

Description: Start the Xfce desktop environment.

Usage: startxfce4

Example: startxfce4 (start the Xfce desktop environment)

24. Enter WSL bash#

Description: Enter the WSL bash environment (Windows).

Usage: wsl -e bash --norc

Example: wsl -e bash --norc (enter the WSL bash environment)

25. Start a Python script#

Description: Start a Python script and redirect the output to a log file.

Usage: nohup python3 -u script_name > log_file_name &

Example: nohup python3 -u downloadImage.py > log0619.log & (start a Python script named "downloadImage.py" and redirect the output to a log file named "log0619.log")

26. View files in a tar package#

Description: View the file list in a tar package.

Usage: tar -tf file_name

Example: tar -tf %s" % tarFile (view the file list in the tar package named "file.tar")

27. Extract a tar package#

Description: Extract a tar package to a specified directory.

Usage: tar -xvf tar_package_name -C target_directory

Example: tar -xvf fb_pic_jpg.tar -C ./fb_pic_jpg (extract the "fb_pic_jpg.tar" to the "./fb_pic_jpg" directory)

28. Compress files or directories into a tar package#

Description: Compress files or directories into a tar package.

Usage: tar -cvf target_tar_package_name source_file/directory

Example: tar -cvf /home/data111/disksdk/wanghaiyang/fastdfs_image_mapper/fb_pic_jpg.tar 20220525/ (compress the "20220525" directory into the "/home/data111/disksdk/wanghaiyang/fastdfs_image_mapper/fb_pic_jpg.tar")

29. View the absolute path of the startup script of a process#

Description: View the absolute path of the startup script of a specified process.

Usage: sudo lsof -p <pid> | grep cwd | tr -s ' ' | cut -d' ' -f9-

Example: sudo lsof -p 1234 | grep cwd | tr -s ' ' | cut -d' ' -f9- (view the absolute path of the startup script of the process with ID 1234)

30. View DNS configuration#

Description: View the DNS configuration of the current system.

Usage: cat /etc/resolv.conf

Example: cat /etc/resolv.conf (view DNS configuration)

31. Set up a proxy#

Description: Set up a system proxy.

Usage: export ALL_PROXY="http://proxy_server_address:port"

Example: export ALL_PROXY="http://172.19.80.1:7890" (set up a proxy)

32. View detailed information of a Docker container#

Description: View detailed information of a Docker container.

Usage: docker inspect container_id

Example: docker inspect f257d69e0035 (view detailed information of a Docker container)

Summary
The records are quite miscellaneous, looking up whatever is encountered. Or when thinking of certain solutions, I would look them up and record them.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.