Table of contents
3-3) Output redirection and append redirection
3-4) Use the vi editor to edit the file
4. Processing of files and folders
4-2) View directory information
4-3) Operations on directories
6. Find the contents of the file
7. User and user group permissions
8. File and Folder Permissions
9. File archiving and compression
1. Switch directory
cd ../ or cd .. |
Switch to the previous directory (relative) |
cd directory name |
Change to the specified directory (absolute) |
pwd |
View the current directory |
cd ~ |
Switch to the current user's home directory
|
2. View system information
fdisk -l |
View a list of disk partitions |
free |
View memory usage |
cat /proc/meminfo |
View memory information |
cat /proc/cpuinfo |
View CPU information |
top command (similar to explorer in window):
top
|
By default, enter the complete interface to view the consumption and process of the system in real time ;
Hit ctrl + C or q to exit
|
top -n 1 | View the current consumption without entering the interface to watch in real time |
3. Text creation and editing
3-1) Create a file
vi /opt/learn.txt |
Create a learn.txt file in the specified directory and enter the vi editing interface |
touch /opt/learn |
Create a learn file in the specified directory. touch simply creates a blank file |
cat > /opt/learn.txt << EOF |
Create a file in the specified directory, and input content on the screen, and finally input EOF to indicate the end of input. If you don't use << EOF to end, you can also press Ctrl+D directly at the end of the input to end. Each repeated execution will overwrite the original content. |
echo "Hello World" > test.txt | Redirect the output of the specified content to a file. If the file does not exist, a file will be created. If it exists, the original content will be overwritten. |
3-2) View files
vi /opt/test.txt |
Output the text content in the vi editor (create one if there is none) |
cat /opt/test.txt |
output text on the screen |
more /opt/test.txt |
Split-screen output text content ( applicable when scrolling is not possible, pre-load all content, press Enter key to display the next line ) |
less /opt/test.txt |
Split-screen output text content (the difference from more is that whichever line you see, it will be loaded to that line, and it is suitable for viewing large files ) |
strings /opt/test.txt |
print printable characters in the file |
head -n 11 /opt/test.txt |
View the first 11 lines of the specified file |
head /opt/test.txt |
View the first 10 lines of the specified file (default 10 lines) |
tail -n 20 /opt/test.txt |
View the last 20 lines of the specified file |
tail /opt/test.txt |
View the last 10 lines of the specified file (the default is 10 lines) |
tail -f /opt/test.txt |
View the latest file content in real time by streaming (real-time monitoring log) |
3-3) Output redirection and append redirection
> Redirection for output (output the result of the previous execution to the specified text, which will overwrite the original content in the text);
>> To add redirection, append the previously executed content to the end of the specified text;
3-4) Use the vi editor to edit the file
# 进入vi编辑器。如果没有这个文本文件,就会自动新建并进入vi编辑器
vi file.txt
After entering the vi editor, it will be in the command mode first , that is, all the input in the current situation is a command; you can click i, a, o, etc. to enter the editing mode ; in the editing mode, press the ESC key to re-enter the command mode .
The following are some commands in command mode of vi editor .
i or a |
Enter from command mode to edit mode; i is to insert text, a is to append text; both input at the cursor position |
ESC |
Enter command mode from edit mode |
:w |
save text without exiting |
:q! |
Force quit without saving text |
:wq |
save text and exit |
o |
Add a new line after the current line |
O |
Add a new line after the previous line of the current line |
dd |
delete a row |
D |
Delete the content from the current cursor to the end of the line |
x |
Delete a character at the current cursor position |
s |
delete a character and switch back to edit mode |
S |
Delete a line and switch back to edit mode |
:n |
Move the cursor to the nth line |
$ |
Move the cursor to the end of the current line of text |
A |
Move the cursor to the end of the line and switch back to edit mode |
^ |
Move the cursor to the beginning of the text line |
G |
Move the cursor to the end of the text |
gg |
Move the cursor to the beginning of the text line |
ZZ |
Save and exit |
/string |
find a string |
n |
keep looking |
:u |
Undo (equivalent to Ctrl+Z) |
:redo |
Redo (equivalent to Ctrl+Y) |
4. Processing of files and folders
4-1) Processing of files
cp - copy | |
cp hello.txt /opt/test |
Copy the hello.txt file to the /opt/test directory |
cp hello.txt /opt/test/he.txt |
把 hello.txt 文件复制一份到 /opt/test 目录下,并重命名为he.txt |
cp /opt/hello.txt ./ |
将指定的文件复制到当前文件夹下 |
rm —— 删除 | |
rm hello.txt |
删除指定文件 |
rm -f hello.txt |
强制删除,不重复确认 |
rm *.txt | 删除当前目录下的所有 .txt 文件 |
mv —— 移动(剪切) | |
mv hello.txt he.txt |
重命名hello.txt 文件为 he.txt |
mv hello.txt /opt/test |
将文件剪切到指定目录下 |
mv hello.txt /opt/test/he.txt |
将文件剪切到指定目录下并重命名为he.txt |
du -h hello.txt |
查看 hello.txt 文件的大小 |
true > test.txt | 清空文件内容 |
echo "" > test.txt | 清空文件内容 |
4-2)查看目录信息
ls 或 ls /opt |
查看当前目录(或指定目录)下没有被隐藏的所有文件(列出文件名称) |
ls -l 或 ll |
以列表的形式查看当前目录下各文件的信息 |
ls -a |
显示当前目录下的所有文件,包括隐藏文件 |
ls *.txt |
显示当前目录下所有以txt为后缀名的文件 |
ls /opt/test |
显示指定目录下的内容 |
ls -R /opt |
显示指定目录及其子目录的内容 |
在linux系统下如果要创建一个隐藏文件,只需要在文件名的最前面加一个点 "." 即可。
ls 直接查询不会查询到隐藏文件。ls -a 查询可以查询到。
4-3)对目录的操作
mkdir folder |
创建指定名称的目录 |
mkdir folder1 folder2 folder3 |
连续创建若干个目录 |
mkdir -p /opt/folder1/folder2/folder3 |
连续创建层次目录(folder1/folder2/folder3) |
rmdir /opt/mydir |
删除空的目录(不能删除非空的目录) |
rm -r ./test |
删除目录 |
rm -rf /opt/folder |
强制删除文件夹 |
cp -r /opt/test1 /opt/test2 |
拷贝文件夹(加上参数-r是深度拷贝) |
mv /opt/test /opt/test2 |
重命名文件夹 |
5、文件查找
find ./ -name "hello.txt" |
查找当前目录下名为hello.txt的文件或文件夹 |
find /home -name "hello" |
查找/home目录下名字为hello的文件或文件夹 |
find ./ -name "*" |
查找当前目录下的所有文件或文件夹 |
find ./ -name "[h|f]*" |
查找当前目录下所有以h或f开头的文件或文件夹 |
find ./ -name "[a-fA-F]*" |
查找当前目录下所有以小写字母a~f或大写字母A~F开头的文件或文件夹 |
find ./ -name "[^a-fA-F]" |
查找当前目录下所有不以a、b、c、d、e、f、A、B、C、D、E、F开头的文件 |
find ./ -name "h?llo" |
查找当前目录下满足h?llo条件的文件,其中?的位置代表一位任意字符 |
find ./ -perm 741 |
查找当前目录下所有权限为741的文件或文件夹 |
find ./ -user mary |
查找所有者为mary的文件或文件夹 |
find ./ -group dev |
查找所属组为dev的文件或文件夹 |
find ./ -mtime -3 |
查找3天内更新过的文件或文件夹 |
find ./ -mtime +3 |
查找3天前更新过的文件或文件夹 |
find ./ -newer hello.txt |
查找比hello.txt更新的文件或文件夹 |
find ./ ! -newer hello.txt |
查找比hello.txt更旧的文件或文件夹 |
find ./ -type d |
查找当前目录下的所有文件夹 ( f:普通文件 d:目录(文件夹) l:软链接文件 c:字符设备,如终端、磁带机等 b:块设备,如光盘、硬盘 ) |
find ./ -size 602c |
查找当前目录下文件大小为602字节的文件 ( c:byte k:kilobytes M:megabytes G:gigabytes ) |
find ./ -size +600c |
查找当前目录下文件大小大于602字节的文件 |
find ./ -name "hello*" -exec ls -l {} \; |
查找当前目录下所有以hello开头的文件,并将其细节显示出来(目录也会) |
find ./ -name "hello*" -exec rm {} \; |
查找当前目录下所有以hello开头的文件并将其删除 |
find ./ -name "hello*" | xargs ls -l |
查找当前目录下所有以hello开头的文件并将其细节显示出来 |
“|” 是管道符,管道符主要用于多重命令处理,前面命令的打印结果作为后面命令的输入。
xargs 的作用是 把前面管道符的输入作为后面指令的参数。
6、查找文件中的内容
grep "root" /etc/passwd |
查找/etc/passwd文件中包含root的行 |
grep -n "root" /etc/passwd |
查找/etc/passwd文件中包含root的行,并在每行的前方标注在原文中的行号 |
grep "^ma" /etc/passwd |
查找以ma开头的行 |
grep "bash$" /etc/passwd |
查找以bash结尾的行 |
grep "^[r|d]" /etc/passwd |
查找以r或d开头的行 |
grep -v "root" |
排除掉包含root在内的行 |
ls | grep test |
从ls的输出中过滤出包含test的行 |
grep -r games /etc |
在/etc目录下查找包含games的文件 |
find ./ name "*" | xargs grep word |
输出当前目录下文件名或目录名包含word的文件或目录 |
wc -l |
统计文件行数或输出的个数。 -c、--bytes、--chars只显示bytes数。 -l、--lines显示行数。 -w、--words只显示字数。 如不指定参数,则显示所有的统计信息 |
7、用户与用户组权限
在 linux 中,root 拥有最高的权限,所有文件或文件夹的权限设定对 root 都是无效的。
在正式的生产和服务器环境中,尽量使用普通的账号+人工权限设定的方式,避免出现漏洞后被人以root权限利用系统。
在 window 中,administrator 和 Linux 中的 root 等同。
cat /etc/group |
查看当前系统存在的用户组,<1000的是系统使用的,>1000的是用户创建的 |
groupadd test |
创建一个用户组名为test |
groupmod -n test testing |
将用户组test更名为testing |
groupdel test |
删除用户组test |
groups root |
查看用户root所在的所有组 |
cat etc/passwd |
查看所有账户 该文件每行代表一个用户账号,记录了账号的所有信息,包括用户名、密码、用户ID(UID)、组ID(GID)、连接、主目录和默认的shell等。 |
useradd -g test jack |
创建一个用户jack,主组归属于test |
useradd -g test jam -G dev |
创建一个用户jam,主组归属于test,附属组dev |
useradd ben |
创建一个用户ben,因为没有设定其归属的主组,默认将新增一个对应名为ben的组 |
usermod -g dev ben |
将用户ben换到dev组中 |
usermod -G 505 ben |
将用户ben附加到gid为505的组中 |
usermod -d /home/test ben |
将用户ben的登入目录改为/home/test (在修改前需要保证该目录存在) |
userdel ben |
删除用户ben |
userdel -f ben |
强制删除用户ben(即使该用户已经登录) |
userdel -r ben |
删除用户ben并删除其主目录 |
8、文件和文件夹权限
在任意的一个文件夹中,可以用ls -l或ll命令来显示其基本的信息和权限信息:
每一行中依次代表的内容如下:
1、文件权限:表示文件的访问权限,如读取、写入和执行权限。
2、链接数量:表示文件在文件系统中的硬链接数量。
3、文件所有者:表示文件的所有者用户及用户所属的组。
4、文件大小:表示文件的大小,以字节为单位。
5、修改时间:表示文件的最后修改时间。
6、文件名:表示文件的名称。
每一个文件都占一行来展示其信息,每一行的前10个字符所表示的意义如下:
如:drwxr-xr-x
第1个字母:‘-’代表普通文件,‘d’代表目录,‘l’为链接文件,相当于快捷方式
第2、3、4个字母代表当前文件或文件夹所属用户(owner)的权限,用u表示
第5、6、7个字母代表当前文件或文件夹所属的用户组(group)的权限,用g表示
地8、9、10个字母代表其他用户组和其他用户(other)的权限,用o表示
权限的表示方式:
r 读:也可以用数字 4 来表示
w 写:也可以用数字 2 来表示
x 执行:也可以用数字 1 来表示
su jame |
切换到jame用户 |
whoami |
查看当前的用户 |
chmod u+x test.txt |
为test.txt文件的所有者添加可执行权限 |
chmod u-w test.txt |
为文件的所有者去除写权限 |
chmod g-r test.txt |
为文件的所在组去除可读权限 |
chmod o+w test.txt |
为文件所在组之外的组添加可写权限 |
chmod a+w test.txt |
为所有角色添加可写权限 |
chmod a+wx test.txt |
为所有角色添加可写和可执行权限 |
chmod a-rwx test.txt |
去除所有用户对文件的读写和执行权限,此时只有root用户可以操作 |
chmod 777 test.txt |
将文件的权限设置为rwxrwxrwx,等同于chmod a-rwx test.txt (7就是4+2+1) |
chmod 643 test.txt |
将文件的权限设置为rw-r---wx |
chmod -R 755 /opt/temp |
将/opt/temp即其所有的子文件、子目录的权限改为755(连带) |
chown mary test.txt |
将test.txt文件的所有者改为mary |
chown mary /opt/temp |
将目录的所有者改为mary |
chown -R mary /opt/temp |
将目录及其子目录、子文件的所有者改为mary |
chgrp test hello.txt |
将hello.txt 所在的组该为test组 |
chgrp test /opt/temp |
将指定目录的所有组改为test组 |
chgrp -R mary /opt/temp |
将指定目录及其下属的目录和文件的所有组改为mary |
9、文件归档和压缩
在Linux中,文件归档的作用是将多个文件组合成一个单一的文件,这个过程称为归档。
它通常用于系统备份、将旧数据移至长期存储设备以及方便文件传输等场景。
tar 命令是用于创建归档文件的常用命令。
归档文件不是文件夹,虽然它们看起来像是文件夹,但实际上它们是单一的文件。可以在这个“文件夹”中放入任何类型的文件,包括文本文件、图片、视频等。归档文件的好处之一是,它们可以轻松地复制和移动到其他位置,而不需要单独管理每个文件。
文件归档后并不会被压缩大小;一般是多个文件一起归档后再进行压缩。
归档后的文件后缀为.tar;归档文件压缩后一般后缀为.tar.gz;
tar -cvf hello.tar ./* |
将当前目录下的所有文件归档为hello.tar; 修饰符c:创建一个新归档 v:详细列出被处理的文件 f:指定要创建的归档文件的文件名或要解压的归档文件的名字。它必须是tar命令的最后一个参数,后面直接接文件名 |
tar -cvf hello.tar /opt/test |
将指定目录下的所有文件归档为hello.tar; |
tar -cf all.tar test1 test2 test3 |
选择当前目录下的三个文件进行归档,归档文件命名为all.tar |
tar -tf test.tar |
将归档文件test.tar中的文件显示出来 |
tar -xvf test.tar |
将归档文件解压缩到当前目录下,恢复其中的文件和目录结构。 |
tar -zcvf hello.tar.gz hello.tar |
将归档文件hello.tar压缩成hello.tar.gz |
tar -zxvf hello.tar.gz |
将压缩文件解压成hello.tar归档文件 |
gzip hello.tar |
将归档文件hello.tar压缩成hello.tar.gz |
gzip -d hello.tar.gz |
将压缩文件解压成hello.tar归档文件 |
zip hello.zip hello.txt |
将hello.txt压缩并命名为hello.zip |
zip -r test.zip /opt/test |
将指定的目录进行压缩并命名为test.zip |
unzip -v hello.zip |
查看压缩文件hello.zip中的文件信息 |
unzip hello.zip |
解压缩hello.zip |