20 Essential Linux Commands for Beginners

introduce:

This beginner-centric article lists all the useful commands for navigation and basic CRUD (create delete update delete) operations on Linux-based operating systems.

What is Linux:

Linux is a UNIX-based operating system (Mac OS is also based on the UNIX operating system). Linux was created by Linus Torvalds as free and open source software. There are several Linux distributions, often referred to as "distros". These include:

  • Debian
  • Ubuntu
  • Fedora
  • RedHat Enterprise Linux (RHEL)
  • Arch Linux
  • Chrome OS

RHEL and Chrome OS are considered commercial distributions. About 90% of the world's servers run on Linux-based distributions. It's safe, free and fast.

While most of the aforementioned operating systems come with decent GUIs, it's important to implement and utilize the potential of command line tools. Commonly referred to as the Linux Shell, the command-line tool reveals access to new features not available with a graphical user interface (GUI). You will always type after the command line (called the command prompt) $

Linux commands:

  1. Get help when you need it:

It is not possible to remember all possible options available for a command. If you haven't used the command before or in a long time, you can choose to view the possible options you can pass. Most commands support passing help as an option and display messages about how to use the command.

<command_name> -- help

 For all possible help, there is the man command. A man page (abbreviated man) is a document that explains what a command does, possible options, example use cases, etc. Here's all the help you can possibly get with the command.

man<command_name>
pwd
/home/username

 3. LS: LS lists all the files that exist in your directory. You can get more information about the file and also use this option to view hidden files.-al

ls -al

4.cd: cd stands for changing directory. If you want to change into another directory, use this command. When used without arguments, cd will take you to the home directory. Absolute or relative paths can be passed to the cd command. Note also that it indicates the current directory and indicates the parent directory.

#将你带到主目录
cd
#进入到父目录
cd ../
#进入到之前的工作目录
cd ~

5.mv: mv (expanded to move) is used to move a file/directory from location 1 to location 2. It can also be used to rename files

#将文件a.txt重命名为b.txt
mv a.txt b.txt
#从当前目录下的test目录移动文件到tmp文件夹
mv test/test.txt /tmp/test.txt
#移动当前目录下多个文件到/tmp
mv a.txt b.txt c.txt /tmp
#通过指定绝对路径将文件从目录a移动到目录b
mv /var/log/test.log /tmp/test.log

6.cp:  Copies the file from location 1 to location 2. You can use this option to copy directories.-R

#复制文件从Home到另一个位置
cp test.txt /tmp/bckup
#递归地复制目录的内容
cp -R test_dir /tmp/bckup

7. rm: RM (extended to delete) is used to delete files or directories. There is no undo when deleting files. So be careful when you want to delete something

#删除当前位置的一个文件
rm /home/test_user/test.txt
#rm 一个空目录
rm -r <location_of_dir>
# rm一个有内容的文件夹
rm -rf <location_of_dir>

8. mkdir: mkdir (expanded to make directory) is used to create a new directory in a specific location.

#创建一个目录
mkdir test_dir
#创建目录(test_directory_child)时创建中间目录(test_directory)
mkdir -p /home/test_user/test_directory/test_directory_child

9. rmdir:  delete the directory. it is一个可供选择的命令 rm -rf

#删除一个文件 test_directory_child
rmdir /home/test_user/test_directory_child
#删除路径中的中间目录,只有当中间目录不包含除指定的目录之外的任何其他子目录#时才会起作用
#指定的目录以外的目录
rmdir -p test_dir/test_dir_child/test_dir_child2

10. cat: Print the contents of the file on the terminal and return to the prompt. You can use various editors to view the content of the file (such as vim, nano, etc.), but this command prints the content to STDOUT (standard output).

#打印文件的内容
cat test.sh

11.touch:  Change file timestamp. Update the timestamp of an existing file, or create the file with the current timestamp if it does not exist.

#如果文件file1.txt不存在,创建一个新的文件
touch file1.txt

12. sudo:  This command is an acronym for superuser execution, which helps you perform tasks that require administrative privileges. However, random use of this command for no reason is not recommended, as any errors made while being root are irreversible.

#读取syslog日志的内容
sudo cat/var/log/syslog

13. find: This command searches the folder hierarchy for files or directories matching the specified name or pattern. It performs a recursive search in all directories under the root directory.

#寻找一个名为test.txt的文件
find /home/test_user -name test.txt
#查找特定模式的文件递归查找
find . -type f -name "*.sh" -R
#查找并删除遵循相同模式的多个文件
find . -type f -name"*.py" -exec rm -f {}

14. grep:  This command prints all lines in a file that match a specific pattern.

#递归地在目录树中的查找模式
grep -r search_field /etc/
#查找两个不同的字母
egrep -w 'x|y' /home/test_user/*

15.df:  This command reports the file system disk space usage.

#查看Linux 系统上的所有文件系统,你可以使用 df 命令加上 -a 选项。-a 选项会显示所有文件系统,包括那些没有挂载的。人类可读格式意味着输出会使用像 KB、MB、GB 等单位,而不是字节。要获得人类可读格式,你可以使用 -h 选项与 df
df -ah

16. du: Estimate file space usage. It has various options to display the output in the format you want.

#显示当前目录中所有文件和目录的磁盘使用情况
du -ah

#指定目录中文件和目录的大小之和
du -sh /home/test_user

17. uname: print system information. If you need specific information about your system, such as kernel version, processor type, hardware platform, etc., you can pass multiple options,

#打印系统的所有信息
uname -a

18. lsblk : lsblk (extended to list block devices) is used to list all block devices in a tree. It also provides information about the partitions present on the block device.

NAME    MAJ:MIN RM   SIZE RO TYPE  MOUNTPOINT
sda       8:0    0  55.9G  0 disk
└─sda1    8:2    0  55.9G  0 part  /
sdb       8:16   0 111.8G  0 disk
└─sdb1    8:17   0 111.8G  0 part

19. hostname: print/set the hostname of the computer. Only superusers can update the hostname

#打印系统的主机名
hostname

#设置主机名
hostname set name <host_name>

20.tail:  Display the last part of the file. By default it will print the last 10 lines if a file is passed

 

#输出附加的数据
tail -f /var/log/syslog

The above are common commands used to introduce the Linux command line and the convenience of beginners. I hope it can help everyone.

Guess you like

Origin blog.csdn.net/qq_61813593/article/details/130353467