[转帖]学习linux必须知道的命令

学习,linux,必须,知道,命令 · 浏览次数 : 0

小编点评

**排版说明:** * 必须使用排版符号或标签,例如 `-`、`=`、``等。 * 必须使用缩排符号,例如 ``、``等。 * 必须使用格式化符号,例如 ``、``等。 **排版示例:** ``` -文件1.txt -文件2.txt ``` **排版说明:** * `-` 表示文件1.txt。 * `` 表示文件2.txt。 * ``表示格式化符号,例如 ``、``等。

正文

https://www.cnblogs.com/aibeier/p/15315487.html

 

基础不牢,地动山摇。
在linux命令行下查看命令帮助
man用于查看命令的帮助信息 man cp
--help cd --help
info查看程序对应文档信息的命令,可以作为man和help命令的帮助补充info ls
从互联网获取帮助 google>bing>baidu
调整中文显示
#echo $LANG
#vi /etc/sysconfig/i18n i18n=internationalization i和n之间18个字母
如果找不到可以编辑
# vi /etc/locale.conf
# cat /etc/locale.conf
LANG="en_US.UTF-8"
#LANG="zh_CN.UTF-8" 临时设置
#source /etc/sysconfig/i18n 文件生效
#echo $LANG
关机重启注销命令
shutdown、init、halt、poweroff、reboot
shutdown -h now 立即关机
shutdown -h +1 1分钟后关机
shutdown -h 11:00 11:00关机
shutdown -r now 立即重启
shutdown -r 11:00 11:00重启
shutdown -c取消
# shutdown -r 11:00
Shutdown scheduled for Wed 2022-01-05 11:00:00 PST, use 'shutdown -c' to cancel.
# shutdown -c
init 0 表示关机
init 6 表示重启
从redhat或centos 6开始,halt/poweroff/reboot三个命令对应的都是同一个man帮助文档,而halt、poweroff、reboot是systemctl命令的链接文件。
halt 立即关机 halt是reboot命令的链接文件
poweroff 立即关机 poweroff也是reboot命令的链接文件
# ls -l /sbin/halt
lrwxrwxrwx. 1 root root 16 7月 18 2018 /sbin/halt -> ../bin/systemctl
# ll /sbin/poweroff
lrwxrwxrwx. 1 root root 16 7月 18 2018 /sbin/poweroff -> ../bin/systemctl
# ll /sbin/reboot
lrwxrwxrwx. 1 root root 16 7月 18 2018 /sbin/reboot -> ../bin/systemctl
reboot有两个参数
--halt
--power-off

设置系统时间
在Linux下,默认情况下,系统时间和硬件时间并不会自动同步。在Linux运行过程中,系统时间和硬件时间以异步的方式运行,互不干扰。硬件时间的运行,是靠BIOS电池来维持,而系统时间,是用CPU Tick来维持的。在系统开机的时候,会自动从BIOS中取得硬件时间,设置为系统时间。

1. Linux系统时间的设置
在Linux中设置系统时间,可以用date命令:
[root@localhost /home/sun]# date
2022年 01月 16日 星期日 06:34:06 PST
[root@localhost /home/sun]# date -s "20220116 22:35:00"
2022年 01月 16日 星期日 22:35:00 PST
2. Linux硬件时间的设置
硬件时间的设置,可以用hwclock或者clock命令。两者基本相同,只用一个就行,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。
[root@localhost /home/sun]# clock --show
2022-01-16 06:44:03.860190-08:00
[root@localhost /home/sun]# clock --set --date "20220116 22:00:00"
[root@localhost /home/sun]# clock
2022-01-16 22:00:03.431578-08:00
3. 系统时间和硬件时间的同步
//以系统时间为基准,修改硬件时间
[root@node1 ~]# hwclock --systohc sys(系统时间)to(写到)hc(Hard Clock)
//或者
[root@node1 ~]# hwclock -w
//以硬件时间为基准,修改系统时间
[root@node1 ~]# hwclock --hctosys
//或者
[root@node1 ~]# hwclock -s

=====================================================
文件和目录操作
pwd 显示当前所在位置
pwd:print working directory
pwd -L 获取环境变量的PWD对应的值,即为echo $PWD的结果
pwd -P 显示链接对应的源文件的目录路径
[root@localhost sbin]# pwd
/sbin
[root@localhost sbin]# pwd -P
/usr/sbin
在Bash命令行显示当前用户的完整路径
echo $PS1 打印超级管理员对应的PS1值
# echo $PS1
[\u@\h \W]\$
PS1='[\u@\h \w]\$' 临时修改
vi /etc/bashrc 永久修改
source /etc/bashrc

cd切换目录
cd:change directory
cd - 切换到当前用户上一次所在目录
cd ~ 切换到当前用户的家目录
cd 不带任何参数,和cd ~结果一样
cd ..切换到上一级目录
cd ../../ 切换到父目录的父目录,只要目录有足够多的层次,可以一直继续下去,直到/为止


tree以树形结构显示目录下的内容
tree 不接选项和目录,默认显示当前所在路径目录的目录结构

如果没有可以安装
rpm -qa tree
yum -y install tree

如果树形显示乱码
LANG=en_US.UTF-8临时解决
tree -a 以树形结构显示目录下的所有内容
tree -L 1 / 只列出根目录下第一层目录的结构
tree -d /etc/ 只显示所有的目录(不显示文件)
tree -L 1 -f /boot/ -f显示内容的完整路径
tree -L 1 -fi /boot/-i不显示树枝,当需要获取所有文件的完整路径时,这个命令很好用
//***************************
[root@localhost ~]# tree /tmp/test
/tmp/test
├── 1.txt
├── 2.txt
├── 3.txt
└── test_1
├── a.txt
├── b.txt
└── c.txt
1 directory, 6 files
[root@localhost ~]# tree -a /tmp/test
/tmp/test
├── 1.txt
├── 2.txt
├── 3.txt
└── test_1
├── a.txt
├── b.txt
└── c.txt
1 directory, 6 files
[root@localhost ~]# tree -L 1 /tmp/test
/tmp/test
├── 1.txt
├── 2.txt
├── 3.txt
└── test_1
1 directory, 3 files
[root@localhost ~]# tree -L 1 -f /tmp/test
/tmp/test
├── /tmp/test/1.txt
├── /tmp/test/2.txt
├── /tmp/test/3.txt
└── /tmp/test/test_1
1 directory, 3 files
[root@localhost ~]# tree -L 1 -fi /tmp/test
/tmp/test
/tmp/test/1.txt
/tmp/test/2.txt
/tmp/test/3.txt
/tmp/test/test_1
[root@localhost ~]# tree -L 1 -dfi /tmp/test
/tmp/test
/tmp/test/test_1
1 directory
[root@localhost ~]# tree -dfi /tmp/test
/tmp/test
/tmp/test/test_1
1 directory
[root@localhost ~]# tree -L 1 -Fi /tmp/test | grep /$
test_1/
***************************//
----------
范例:
使用tree命令区分目录和文件的方法
tree -L 1 -F /boot/ 使用-F参数会在目录后面添加/,方便区分目录
tree -L 1 -F /boot/ | grep /$ 过滤以斜线结尾的所有内容
----------

mkdir创建目录
mkdir:make directories
mkdir data 当前目录下创建data
mkdir -p data/ptest 参数-p递归创建多级目录
tree -d
mkdir -pv data/pvtest 参数-v显示创建目录过程
mkdir -m 333 datam 创建目录时指定333的数字权限
同时创建多个目录及多级子目录
mkdir -pv data/{dir_1,dir_2}/{dir_2_1,dir2_2}
mkdir -p data/dir{1..5} data2/dir{a..c}
//**************************************
[root@localhost hometest]# mkdir -p test_1/test_2
[root@localhost hometest]# tree -d
.
└── test_1
└── test_2
2 directories
[root@localhost hometest]# mkdir -pv test_3/test_4
mkdir: 已创建目录 'test_3'
mkdir: 已创建目录 'test_3/test_4'
[root@localhost hometest]# mkdir -m 333 test_5
[root@localhost hometest]# ls -ltr
总用量 0
drwxr-xr-x. 3 root root 20 1月 5 07:09 test_1
drwxr-xr-x. 3 root root 20 1月 5 07:10 test_3
d-wx-wx-wx. 2 root root 6 1月 5 07:10 test_5
**************************************//
-----------
克隆目录结构:对文件目录做操作时,预先建立的目录结构
tree -fid --noreport /tmp/data 显示所有目录树 --noreport不显示最后一行的统计信息
tree -fid --noreport /tmp/data >> ./tongji.txt
cd /root/
mkdir -p `cat /tmp/data/tongji.txt`
tree -d /root/
-----------
//**************************************
[root@localhost hometest]# cd /root/hometest/
[root@localhost hometest]# tree -fid --noreport
.
./test_1
./test_1/test_2
./test_3
./test_3/test_4
./test_5
[root@localhost hometest]# tree -fid --noreport > /root/bk.txt
[root@localhost hometest]# cat /root/bk.txt
.
./test_1
./test_1/test_2
./test_3
./test_3/test_4
./test_5
[root@localhost hometest]# cd /root/hometest_bk/
[root@localhost hometest_bk]# pwd
/root/hometest_bk
[root@localhost hometest_bk]# mkdir -pv `cat /root/bk.txt`
mkdir: 已创建目录 './test_1'
mkdir: 已创建目录 './test_1/test_2'
mkdir: 已创建目录 './test_3'
mkdir: 已创建目录 './test_3/test_4'
mkdir: 已创建目录 './test_5'
[root@localhost hometest_bk]# ls -ltr
总用量 0
drwxr-xr-x. 3 root root 20 1月 5 07:21 test_1
drwxr-xr-x. 2 root root 6 1月 5 07:21 test_5
drwxr-xr-x. 3 root root 20 1月 5 07:21 test_3
[root@localhost hometest_bk]# tree -d
.
├── test_1
│   └── test_2
├── test_3
│   └── test_4
└── test_5
5 directories
**************************************//

touch创建空文件或改变文件的时间戳属性
mkdir是创建空文件夹,touch是创建空文件
stat a.txt 查看文件的时间戳属性
访问时间、修改时间、状态改变时间
Access: 2021-11-11 11:11:00.000000000 +0800
Modify: 2021-11-11 11:11:00.000000000 +0800
Change: 2021-12-31 21:38:49.346894400 +0800

touch -a a.txt 修改最后访问时间(access)
touch -m a.txt 更改最后修改时间(modify)
状态改变时间自动更新

touch -d 指定创建文件后的文件修改时间modify
touch -d 20201001 b.txt
ls -lh --full-time b.txt
touch -r 修改b.txt的时间属性(access.modify),使其和a.txt的时间属性一致
touch -r a.txt b.txt
touch -t 指定文件的最后修改时间(access.modify)
touch -t 20220101010101.50 b.txt

ls -lu 最后访问时间 atime access time
ls -lt 最后修改时间 mtime modify time
ls -lc 状态改变时间 ctime change time

touch -d 时间格式YYYYMMDD 不能指定时分秒
touch -t 时间格式YYYYMMDDHHMMSS 不能只指定YMD
//**************************************
[root@localhost ~]# stat bk.txt
文件:bk.txt
大小:61 块:8 IO 块:4096 普通文件
设备:805h/2053d Inode:34144160 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-01-05 07:20:55.526506445 -0800
最近更改:2022-01-05 07:20:51.791467587 -0800
最近改动:2022-01-05 07:20:51.791467587 -0800
创建时间:-
[root@localhost ~]# touch -a bk.txt
[root@localhost ~]# stat bk.txt
文件:bk.txt
大小:61 块:8 IO 块:4096 普通文件
设备:805h/2053d Inode:34144160 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-01-05 07:26:32.092293713 -0800
最近更改:2022-01-05 07:20:51.791467587 -0800
最近改动:2022-01-05 07:26:32.092293713 -0800
创建时间:-

[root@localhost ~]# touch -m bk.txt
[root@localhost ~]# stat bk.txt
文件:bk.txt
大小:61 块:8 IO 块:4096 普通文件
设备:805h/2053d Inode:34144160 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-01-05 07:26:32.092293713 -0800
最近更改:2022-01-05 07:27:04.114742348 -0800
最近改动:2022-01-05 07:27:04.114742348 -0800
创建时间:-

[root@localhost ~]# touch -d 20201001 bk.txt
[root@localhost ~]# stat bk.txt
文件:bk.txt
大小:61 块:8 IO 块:4096 普通文件
设备:805h/2053d Inode:34144160 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2020-10-01 00:00:00.000000000 -0700
最近更改:2020-10-01 00:00:00.000000000 -0700
最近改动:2022-01-05 07:28:02.772537924 -0800
创建时间:-

[root@localhost ~]# touch a.txt
[root@localhost ~]# stat a.txt
文件:a.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:805h/2053d Inode:34144162 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-01-05 07:28:58.380247213 -0800
最近更改:2022-01-05 07:28:58.380247213 -0800
最近改动:2022-01-05 07:28:58.380247213 -0800
创建时间:-
[root@localhost ~]# touch -r a.txt bk.txt
[root@localhost ~]# ls -lh --full-time a.txt bk.txt
-rw-r--r--. 1 root root 0 2022-01-05 07:28:58.380247213 -0800 a.txt
-rw-r--r--. 1 root root 61 2022-01-05 07:28:58.380247213 -0800 bk.txt

[root@localhost ~]# touch -t 202201061111.50 a.txt
[root@localhost ~]# ls -lh --full-time a.txt
-rw-r--r--. 1 root root 0 2022-01-06 11:11:50.000000000 -0800 a.txt

[root@localhost ~]# stat a.txt
文件:a.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:805h/2053d Inode:34144162 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:admin_home_t:s0
最近访问:2022-01-06 11:11:50.000000000 -0800
最近更改:2022-01-06 11:11:50.000000000 -0800
最近改动:2022-01-05 07:31:20.710062668 -0800
创建时间:-
[root@localhost ~]# ls -lt --full-time a.txt
-rw-r--r--. 1 root root 0 2022-01-06 11:11:50.000000000 -0800 a.txt
[root@localhost ~]# ls -lc --full-time a.txt
-rw-r--r--. 1 root root 0 2022-01-05 07:31:20.710062668 -0800 a.txt
[root@localhost ~]# ls -lu --full-time a.txt
-rw-r--r--. 1 root root 0 2022-01-06 11:11:50.000000000 -0800 a.txt
**************************************//
ls 列出目录内容及其内容属性信息
list directory contents类似Dos系统下的dir命令

ls 直接执行,显示所有文件和目录(不显示隐藏文件)
ls -a 显示所有文件,特别是隐藏文件
ls -l 显示详细信息(文件类型 权限 连接数 属主(组) 大小 创建修改时间)
ls -ltr 其中的-t按时间顺序,-r倒序排列。最新更改的在最后一行(时间最大)
ls -ltr --time-style=long-iso /etc | tail -n 3

※在linux系统中,以·开头的文件就是隐藏文件
touch .hide.txt
mkdir -pv.hide/test

[sun@localhost ~]$ ls -l
drwxrwxr-x. 2 sun sun 6 1月 11 06:15 dir1
-rw-r--r--. 1 root root 0 1月 5 05:53 test.txt

ls -l --time-style=long-iso显示完整时间属性
--time-style=iso 显示月日时分
--time-style=full-iso等效于 --full-time
--time-style=locale 默认模式

[sun@localhost ~]$ ls -l --time-style=long-iso
drwxrwxr-x. 2 sun sun 6 2022-01-11 06:15 dir1
-rw-r--r--. 1 root root 0 2022-01-05 05:53 test.txt
[sun@localhost ~]$ ls -l --time-style=iso
drwxrwxr-x. 2 sun sun 6 01-11 06:15 dir1
-rw-r--r--. 1 root root 0 01-05 05:53 test.txt
[sun@localhost ~]$ ls -l --time-style=full-iso
drwxrwxr-x. 2 sun sun 6 2022-01-11 06:15:35.152121477 -0800 dir1
-rw-r--r--. 1 root root 0 2022-01-05 05:53:14.234597400 -0800 test.txt
[sun@localhost ~]$ ls -l --time-style=locale
drwxrwxr-x. 2 sun sun 6 1月 11 06:15 dir1
-rw-r--r--. 1 root root 0 1月 5 05:53 test.txt
[sun@localhost ~]$ ls -l --full-time
drwxrwxr-x. 2 sun sun 6 2022-01-11 06:15:35.152121477 -0800 dir1
-rw-r--r--. 1 root root 0 2022-01-05 05:53:14.234597400 -0800 test.txt

※若显示时间格式参数过长,可以设置别名
[root@localhost /tmp]# alias | grep ls
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
[root@localhost /tmp]# alias lst='ls -l --time-style=long-iso'
[root@localhost /tmp]# lst
总用量 0
drwxr-xr-x. 2 root root 40 2022-01-11 06:39 dir1
-rw-r--r--. 1 root root 0 2021-11-11 20:20 stat.txt

ls -l --time=atime 访问时间
(×没有这个参数)--time=mtime modify修改时间
--time=ctime change状态改变时间

[sun@localhost ~]$ ls -l --time=atime
总用量 0
drwxrwxr-x. 2 sun sun 6 1月 11 06:15 dir1
-rw-r--r--. 1 root root 0 1月 5 05:53 test.txt
[sun@localhost ~]$ cat test.txt
[sun@localhost ~]$ ls -l --time=atime
总用量 0
drwxrwxr-x. 2 sun sun 6 1月 11 06:15 dir1
-rw-r--r--. 1 root root 0 1月 11 06:25 test.txt

※cat查看文件即表示访问了文件,后续再cat atime不变

ls -F 与tree -F参数类似,都是目录后面加上/,若不是目录,其他类型文件的情况下,会是别的符号。
加上*代表可执行的普通文件
加上/代表目录
加上=表示套接字(sockets)
加上|表示FIFOs
加上@表示符号链接

[root@localhost /home/sun]# ls -F
dir1/ test.txt
[root@localhost /home/sun]# ls -p 等价于-F,功能只是在目录后面加上/
dir1/ test.tx
[root@localhost /home/sun]# ls -F | grep / 过滤目录
dir1/
[root@localhost /home/sun]# ls -F | grep -v / 过滤普通文件
test.txt

[root@localhost /var/spool/mail]# ls -l /etc/init.d
lrwxrwxrwx. 1 root root 11 7月 22 2018 /etc/init.d -> rc.d/init.d
[root@localhost /var/spool/mail]# ls -F /etc/init.d
/etc/init.d@ 链接文件的结尾是@
[root@localhost /var/spool/mail]# ls -F /bin/date
/bin/date* 命令结尾是*
[root@localhost /home/sun]# find / -type s -exec ls -lF {} \;
srwxr-xr-x. 1 root root 0 1月 11 04:59 /run/NetworkManager/private-dhcp=
srw-rw-rw-. 1 root root 0 1月 11 04:59 /run/abrt/abrt.socket=
[root@localhost /home/sun]# find / -type p -exec ls -lF {} \;
prw-------. 1 root root 0 1月 11 04:59 /run/initctl|

ls -d 查看目录本身信息

[root@localhost /tmp]# ls -l dir1
[root@localhost /tmp]# ls -ld dir1
drwxr-xr-x. 2 root root 40 1月 11 06:39 dir1

***************************************
数据库备份获取数据库名列表
[root@localhost /tmp]# ls -F /etc | egrep "/" | awk -F "/" '{print $1}'
abrt
alternatives
audit
...

ls命令输出内容属性解读:
[root@localhost /home/sun]# ls -lhi
16898681 drwxrwxr-x. 2 sun sun 6 1月 11 06:15 dir1
34143319 -rw-r--r--. 1 root root 6 1月 11 06:30 test.txt
第一列inode索引节点编号
第二列文件类型及权限
第三列硬链接个数
第四列所属用户
第五列所属组别
第六列文件或目录的大小
第七八九列是文件或目录的修改时间
第十列实际文件或目录名

//**************************************
cp 复制文件或目录copy

cp -a 等同于-p -d -r 三个选项的总和
cp -p 复制文件时保持源文件的所有者、权限信息及时间属性
cp -d 如果复制的源文件是符号链接,仅复制符号链接本身,并保留符号链接所指向的目标文件或目录
cp -r 递归复制目录
cp -i 覆盖已有文件前提示用户确认,系统默认alias cp = 'cp -i'
cp -t 目标文件 源文件 = cp 源文件 目标文件

[root@localhost /home/sun]# cp -rt test/ dir1.bk
[root@localhost /home/sun]# tree
.
├── 20220111.txt
├── dir1
├── dir1.bk
├── test
│   ├── dir1
│   └── dir1.bk
└── test.txt
[root@localhost /home/sun]# which cp
alias cp='cp -i'
/usr/bin/cp

--覆盖之前不提示的几种方法:

[root@localhost /home/sun]# cp 20220111.txt test.txt
cp:是否覆盖'test.txt'? y
[root@localhost /home/sun]# tree
.
├── 20220111.txt
├── dir1
├── dir1.bk
├── test
│   ├── dir1
│   └── dir1.bk
└── test.txt
5 directories, 2 files
①反斜线\cp 屏蔽系统别名
[root@localhost /home/sun]# \cp 20220111.txt test.txt
②使用cp命令的绝对路径
[root@localhost /home/sun]# /usr/bin/cp 20220111.txt test.txt
③取消别名
[root@localhost /home/sun]# unalias cp
[root@localhost /home/sun]# which cp
/usr/bin/cp
④修改配置文件
[root@localhost /home/sun]# cat ~/.bashrc
# .bashrc
# User specific aliases and functions
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

【快速备份文件案例:】

[root@localhost /home/sun]# ls -lh --full-time
总用量 24K
-rw-r--r--. 1 root root 11K 2022-01-11 07:13:32.089588891 -0800 20220111.txt
drwxrwxr-x. 2 sun sun 6 2022-01-16 05:37:32.730988540 -0800 dir1
drwxr-xr-x. 2 root root 6 2022-01-16 05:38:46.590903372 -0800 dir1.bk
drwxr-xr-x. 4 root root 33 2022-01-16 05:39:07.558416356 -0800 test
-rw-r--r--. 1 root root 11K 2022-01-16 05:43:25.730773918 -0800 test.txt
[root@localhost /home/sun]# cp test.txt test.txt.bk
[root@localhost /home/sun]# cp test.txt{,.bk2}
[root@localhost /home/sun]# ls -lh --time-style=long-iso
总用量 48K
-rw-r--r--. 1 root root 11K 2022-01-11 07:13 20220111.txt
drwxrwxr-x. 2 sun sun 6 2022-01-16 05:37 dir1
drwxr-xr-x. 2 root root 6 2022-01-16 05:38 dir1.bk
drwxr-xr-x. 4 root root 33 2022-01-16 05:39 test
-rw-r--r--. 1 root root 11K 2022-01-16 05:43 test.txt
-rw-r--r--. 1 root root 11K 2022-01-16 05:47 test.txt.bk
-rw-r--r--. 1 root root 11K 2022-01-16 05:48 test.txt.bk2

※cp test.txt{,.bk2}方法原理是bash对大括号的展开操作,test.txt{,.bk2}展开成 test.txt test.txt.bk2再传给cp命令
**************************************//
//**************************************
mv 移动或重命名文件。move/rename files

mv -f 若目标文件已经存在,不询问直接覆盖
mv -i 若文件已经存在,询问是否覆盖
mv -n 不覆盖已经存在的文件
mv -t 和cp -t 功能类似
mv -u 在源文件比目标文件新,或目标文件不存在时才进行移动

rename files:
[root@localhost /home/sun]# which mv
alias mv='mv -i'
/usr/bin/mv
[root@localhost /home/sun]# mv dir1.bk dir2
[root@localhost /home/sun]# ls -lh
总用量 48K
-rw-r--r--. 1 root root 11K 1月 11 07:13 20220111.txt
drwxrwxr-x. 2 sun sun 6 1月 16 05:37 dir1
drwxr-xr-x. 2 root root 6 1月 16 05:38 dir2
drwxr-xr-x. 4 root root 33 1月 16 05:39 test
-rw-r--r--. 1 root root 11K 1月 16 05:43 test.txt
-rw-r--r--. 1 root root 11K 1月 16 05:47 test.txt.bk
-rw-r--r--. 1 root root 11K 1月 16 05:48 test.txt.bk2
[root@localhost /home/sun]# mv test.txt test.txt.bk2
mv:是否覆盖'test.txt.bk2'? y
[root@localhost /home/sun]# ls -lh
总用量 36K
-rw-r--r--. 1 root root 11K 1月 11 07:13 20220111.txt
drwxrwxr-x. 2 sun sun 6 1月 16 05:37 dir1
drwxr-xr-x. 2 root root 6 1月 16 05:38 dir2
drwxr-xr-x. 4 root root 33 1月 16 05:39 test
-rw-r--r--. 1 root root 11K 1月 16 05:47 test.txt.bk
-rw-r--r--. 1 root root 11K 1月 16 05:43 test.txt.bk2

move files:
[root@localhost /home/sun]# tree
.
├── 20220111.txt
├── dir1
├── dir2
├── test
│   ├── dir1
│   └── dir1.bk
├── test.txt.bk
└── test.txt.bk2
5 directories, 3 files
[root@localhost /home/sun]# mv dir2 test/dir1/
[root@localhost /home/sun]# tree
.
├── 20220111.txt
├── dir1
├── test
│   ├── dir1
│   │   └── dir2
│   └── dir1.bk
├── test.txt.bk
└── test.txt.bk2
[root@localhost /home/sun]# mv -t dir1/ test.txt.*
[root@localhost /home/sun]# tree
.
├── 20220111.txt
├── dir1
│   ├── test.txt.bk
│   └── test.txt.bk2
└── test
├── dir1
│   └── dir2
└── dir1.bk
**************************************//
//**************************************
rm 删除文件或目录 remove files or directories
这是linux中最危险的命令之一,慎用。删除之前务必备份

rm -r 递归删除目录及其内容
rm -f 强制删除
rm -i 删除前提示,系统默认
rm -I 在删除超过三个文件或者递归删除前要求确认

[root@localhost /home/sun/dir1]# which rm
alias rm='rm -i'
/usr/bin/rm
[root@localhost /home/sun]# ls -lh
总用量 0
[root@localhost /home/sun]# mkdir -p dir{1..3}
[root@localhost /home/sun]# touch file{1..3}.txt
[root@localhost /home/sun]# tree
.
├── dir1
├── dir2
├── dir3
├── file1.txt
├── file2.txt
└── file3.txt
3 directories, 3 files
[root@localhost /home/sun]# rm file3.txt
rm:是否删除普通空文件 'file3.txt'?n
[root@localhost /home/sun]# rm -f file3.txt
[root@localhost /home/sun]# ls -lh
总用量 0
drwxr-xr-x. 2 root root 6 1月 16 06:11 dir1
drwxr-xr-x. 2 root root 6 1月 16 06:11 dir2
drwxr-xr-x. 2 root root 6 1月 16 06:11 dir3
-rw-r--r--. 1 root root 0 1月 16 06:11 file1.txt
-rw-r--r--. 1 root root 0 1月 16 06:11 file2.txt
[root@localhost /home/sun]# mkdir -p dir1/a/b
[root@localhost /home/sun]# rm dir1
rm: 无法删除'dir1': Is a directory
[root@localhost /home/sun]# rm -r dir1
rm:是否进入目录'dir1'? y
rm:是否进入目录'dir1/a'? y
rm:是否删除目录 'dir1/a/b'?y
rm:是否删除目录 'dir1/a'?y
rm:是否删除目录 'dir1'?y
[root@localhost /home/sun]# rm -rf dir2
[root@localhost /home/sun]# tree
.
├── dir3
├── file1.txt
└── file2.txt
1 directory, 2 files

---补充:
linux rm -rf误删数据后恢复方法

**************************************//
//**************************************
rmdir 删除空目录 remove empty directories
当目录不为空时,命令不起作用。实际工作中使用极少
**************************************//
//**************************************
ln 创建文件之间的链接,make links between files,链接类型包括硬链接hardlink和软连接(符号链接,symbolic link)

ln 源文件 目标文件(硬链接生成的是普通文件 -字符)
ln -s 源文件 目标文件

[root@localhost /home/sun]# ln file1.txt file
[root@localhost /home/sun]# ls -lh
总用量 0
drwxr-xr-x. 2 root root 6 1月 16 06:11 dir3
-rw-r--r--. 2 root root 0 1月 16 06:11 file
-rw-r--r--. 2 root root 0 1月 16 06:11 file1.txt
-rw-r--r--. 1 root root 0 1月 16 06:11 file2.txt
[root@localhost /home/sun]# stat file
文件:file
大小:0 块:0 IO 块:4096 普通空文件
设备:805h/2053d Inode:34134067 硬链接:2
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2022-01-16 06:11:42.047874157 -0800
最近更改:2022-01-16 06:11:42.047874157 -0800
最近改动:2022-01-16 06:29:52.166152076 -0800
创建时间:-
[root@localhost /home/sun]# stat file1.txt
文件:file1.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:805h/2053d Inode:34134067 硬链接:2
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2022-01-16 06:11:42.047874157 -0800
最近更改:2022-01-16 06:11:42.047874157 -0800
最近改动:2022-01-16 06:29:52.166152076 -0800
创建时间:-

[root@localhost /home/sun]# ln -s file2.txt file2
[root@localhost /home/sun]# stat file2
文件:file2 -> file2.txt
大小:9 块:0 IO 块:4096 符号链接
设备:805h/2053d Inode:34143313 硬链接:1
权限:(0777/lrwxrwxrwx) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2022-01-16 06:32:48.977393931 -0800
最近更改:2022-01-16 06:32:48.977393931 -0800
最近改动:2022-01-16 06:32:48.977393931 -0800
创建时间:-
[root@localhost /home/sun]# stat file2.txt
文件:file2.txt
大小:0 块:0 IO 块:4096 普通空文件
设备:805h/2053d Inode:34134070 硬链接:1
权限:(0644/-rw-r--r--) Uid:( 0/ root) Gid:( 0/ root)
环境:unconfined_u:object_r:user_home_t:s0
最近访问:2022-01-16 06:11:42.047874157 -0800
最近更改:2022-01-16 06:11:42.047874157 -0800
最近改动:2022-01-16 06:11:42.047874157 -0800
创建时间:-
[root@localhost /home/sun]# ls -lh
总用量 0
drwxr-xr-x. 2 root root 6 1月 16 06:11 dir3
-rw-r--r--. 2 root root 0 1月 16 06:11 file
-rw-r--r--. 2 root root 0 1月 16 06:11 file1.txt
lrwxrwxrwx. 1 root root 9 1月 16 06:32 file2 -> file2.txt
-rw-r--r--. 1 root root 0 1月 16 06:11 file2.txt

**************************************//
//**************************************

 

**************************************//
//**************************************

 

**************************************//
//**************************************

 

**************************************//
//**************************************

 

**************************************//
//**************************************

 

**************************************//
//**************************************

 

**************************************//

与[转帖]学习linux必须知道的命令相似的内容:

[转帖]学习linux必须知道的命令

https://www.cnblogs.com/aibeier/p/15315487.html 基础不牢,地动山摇。在linux命令行下查看命令帮助man用于查看命令的帮助信息 man cp--help cd --helpinfo查看程序对应文档信息的命令,可以作为man和help命令的帮助补充in

[转帖]【学习笔记】Linux下CPU性能评估

Linux下CPU性能评估 1、 vmstat监控CPU使用情况 【说明】 procs: l r表示运行和等待CPU时间片的进程数,这个值如果长期大于系统CPU的个数,就说明CPU不足,需要增加CPU。 l b表示在等待资源的进程数,比如正在等待I/O或者内存交换等。 memory: l swpd:

[转帖]perf学习-linux自带性能分析工具

存储技术为满足层出不穷应用的海量数据存储需求,从物理介质到技术架构也同样发生了天翻地覆的变革。无论技术如何更新换代,其目的都是为了更好的提供高性能,高容量,高可用的数据服务。本系列文章会对存储系统的测试和调试工具做一个介绍。 dd - Linux世界中的搬运工 FIO – IO压力测试工具 vdbe

[转帖]perf学习-linux自带性能分析工具

目前在做性能分析的事情,之前没怎么接触perf,找了几篇文章梳理了一下,按照问题的形式记录在这里。 方便自己查看。 什么是perf? linux性能调优工具,32内核以上自带的工具,软件性能分析。在2.6.31及后续版本的Linux内核里,安装perf非常的容易。 几乎能够处理所有与性能相关的事件。

[转帖]perf学习-linux自带性能分析工具

目前在做性能分析的事情,之前没怎么接触perf,找了几篇文章梳理了一下,按照问题的形式记录在这里。 方便自己查看。 什么是perf? linux性能调优工具,32内核以上自带的工具,软件性能分析。在2.6.31及后续版本的Linux内核里,安装perf非常的容易。 几乎能够处理所有与性能相关的事件。

[转帖]Linux 学习记录四(Bash 和 Shell scirpt).

https://www.cnblogs.com/jmcui/p/7194627.html 阅读目录 一、什么是 Shell? 二、Bash Shell 的基本操作技巧 三、Shell Script 回到顶部 一、什么是 Shell? 狭义的shell指的是指令列方面的软件,包括基本的Linux操作窗

[转帖]【Linux学习】awk内置函数详细介绍(实例)

https://www.cnblogs.com/gtea/p/12668736.html 这节详细介绍awk内置函数,主要分以下3种类似:算数函数、字符串函数、其它一般函数、时间函数 一、算术函数: 以下算术函数执行与 C 语言中名称相同的子例程相同的操作: 函数名 说明 atan2( y, x )

[转帖]linux学习:sed与awk与tr用法整理

https://www.cnblogs.com/LO-gin/p/6882490.html 流编辑器:sed 语法:sed [-hnV][-e

[转帖]Linux学习14-ab报错apr_pollset_poll: The timeout specified has expired (70007)

https://www.cnblogs.com/yoyoketang/p/10255100.html 前言 使用ab压力测试时候出现报错apr_pollset_poll: The timeout specified has expired (70007),本篇总结了几个ab常见的报错和对应解决办法当

[转帖]Linux 学习笔记: shell中${} 的用法,删除&替换

Linux 学习笔记: shell中${} 的用法,删除&替换 字符串的删除 echo${i##*/} 删除 / 前的所有内容 ## 删除 tt=$i echo{tt:22} #取的22位以后的所有字符 file=/dir1/dir2/dir3/my.file.txt ${file#/}:删掉第一个