Linux文件目录命令

pwd 命令

pwd (Print Working Directory)命令用于显示用户当前所处的工作目录。

[root@localhost ~]# pwd
/root
cd 命令

cd(Change Directory)命令用于切换工作路径,格式为cd [目录名称]

其中使用“cd -命令返回到上一次所处的目录,使用“cd ..命令进入上级目录,以及使用cd ~或者直接cd命令切换到当前用户的家目录。

[root@localhost ~]# cd /root/Documents/
[root@localhost Documents]# cd -
/root
[root@localhost ~]# cd ../
[root@localhost /]# cd -
/root
[root@localhost ~]# cd ~
[root@localhost ~]# cd /
[root@localhost /]# cd
[root@localhost ~]#
ls 命令

ls (list)命令用于显示目录中的文件信息,格式为ls [选项] [文件]

所处的工作目录不同,当前工作目录下的文件肯定也不同。使用ls命令的“-a”参数看到全部文件(包括隐藏文件),使用“-l”参数可以查看文件的属性、大小等详细信息。

[root@localhost ~]# ls -al
total 60
dr-xr-x---. 14 root root 4096 May  6 14:15 .
drwxr-xr-x. 17 root root 4096 May  6 14:02 ..
-rw-------.  1 root root  310 May  4 23:21 .ICEauthority
-rw-------.  1 root root 2097 May  6 14:41 .bash_history
-rw-r--r--.  1 root root   18 Dec 29  2013 .bash_logout
-rw-r--r--.  1 root root  176 Dec 29  2013 .bash_profile
-rw-r--r--.  1 root root  176 Dec 29  2013 .bashrc
drwx------.  9 root root 4096 May  4 23:21 .cache
drwx------. 15 root root 4096 May  4 23:21 .config
-rw-r--r--.  1 root root  100 Dec 29  2013 .cshrc
drwx------.  3 root root   24 May  4 23:20 .dbus
-rw-------.  1 root root   16 May  4 23:21 .esd_auth
drwxr-xr-x.  3 root root   18 May  4 23:21 .local
-rw-r--r--.  1 root root  129 Dec 29  2013 .tcshrc
drwxr-xr-x.  2 root root    6 May  4 23:21 Desktop
drwxr-xr-x.  2 root root    6 May  4 23:21 Documents
drwxr-xr-x.  2 root root    6 May  4 23:21 Downloads
drwxr-xr-x.  2 root root    6 May  4 23:21 Music
drwxr-xr-x.  2 root root    6 May  4 23:21 Pictures
drwxr-xr-x.  2 root root    6 May  4 23:21 Public
drwxr-xr-x.  2 root root    6 May  4 23:21 Templates
drwxr-xr-x.  2 root root    6 May  4 23:21 Videos
-rw-------.  1 root root 1211 May  4 22:43 anaconda-ks.cfg
-rw-r--r--.  1 root root 1262 May  4 23:20 initial-setup-ks.cfg
-rw-r--r--.  1 root root  386 May  6 14:09 test.txt
cat 命令

cat (Concatenate)命令用于查看纯文本文件(内容较少的),格式为cat [选项] [文件]

[root@localhost ~]# cat -n test.txt
     1  Hello world
head 命令

head命令用于查看纯文本文档的前N行,格式为head [选项] [文件]

[root@localhost ~]# head -n 10 anaconda-ks.cfg
#version=RHEL7
# System authorization information
auth --enableshadow --passalgo=sha512

# Use CDROM installation media
cdrom
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=vda
# Keyboard layouts
tail 命令

tail命令用于查看纯文本文档的后N行或持续刷新内容,格式为tail [选项] [文件]

我们可能还会遇到另外一种情况,比如需要查看文本内容的最后20行,这时就需要用到tail命令了。tail命令的操作方法与head命令非常相似,只需要执行tail -n 20 文件名命令就可以达到这样的效果。tail命令最强悍的功能是可以持续刷新一个文件的内容,当想要实时查看最新日志文件时,这特别有用,此时的命令格式为tail -f 文件名

[root@localhost ~]# tail -n 10 anaconda-ks.cfg
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@multimedia
@print-client
@x11

%end

[root@localhost ~]# tail -f anaconda-ks.cfg
@guest-agents
@guest-desktop-agents
@input-methods
@internet-browser
@multimedia
@print-client
@x11

%end
tr 命令

tr (Translate)命令用于替换文本文件中的字符,格式为tr [原始字符] [目标字符]

把某个文本内容中的英文全部替换为大写:

[root@localhost ~]# cat test.txt
Hello world
[root@localhost ~]# cat test.txt | tr [a-z] [A-Z]
HELLO WORLD
wc 命令

wc (Words Counts)命令用于统计指定文本的行数、字数、字节数,格式为wc [参数] 文本

参数作用
-l只显示行数
-w只显示单词数
-c只显示字节数
[root@localhost ~]# wc -l test.txt
1 test.txt
[root@localhost ~]# wc -c test.txt
12 test.txt
[root@localhost ~]# wc -w test.txt
2 test.txt
stat 命令

stat (status)命令用于查看文件的具体存储信息和时间等信息,格式为stat 文件名称

[root@localhost ~]# stat test.txt
  File: 'test.txt'
  Size: 12              Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 70922632    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-05-07 13:16:25.297639318 +0800  最近查看文件时间
Modify: 2020-05-07 13:16:21.019687239 +0800  最近修改文件内容时间
Change: 2020-05-07 13:16:21.019687239 +0800  最近修改文件属性时间
 Birth: -
cut 命令

cut命令用于按“列”提取文本字符,格式为cut [参数] 文本

一般而言,按基于“行”的方式来提取数据是比较简单的,只需要设置好要搜索的关键词即可。但是如果按列搜索,不仅要使用-f (–fields)参数来设置需要看的列数,还需要使用-d (–delimiter)参数来设置间隔符号。/etc/passwd在保存用户数据信息时,用户信息的每一项值之间是采用冒号来间隔的,接下来我们使用下述命令尝试提取出/etc/passwd文件中的用户名信息,即提取以冒号:为间隔符号的第一列内容:

[root@localhost ~]# cut -d: -f1 /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
dbus
polkitd
unbound
colord
usbmuxd
avahi
avahi-autoipd
libstoragemgmt
saslauth
qemu
rpc
rpcuser
nfsnobody
rtkit
radvd
ntp
chrony
abrt
pulse
gdm
gnome-initial-setup
postfix
sshd
tcpdump
diff 命令

diff (Differ)命令用于比较多个文本文件的差异,格式为diff [参数] 文件

[root@localhost ~]# diff test.txt test2.txt
1c1
< Hello world
---
> Hello World
touch 命令

touch命令用于创建空白文件或设置文件的时间,格式为touch [选项] [文件]

参数作用
-a仅修改“读取时间”(atime)
-m仅修改“修改时间”(mtime)
-d同时修改atime与mtime
[root@localhost ~]# touch test3.txt
[root@localhost ~]# stat test3.txt
  File: 'test3.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 70922637    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-05-07 13:44:10.679777123 +0800
Modify: 2020-05-07 13:44:10.679777123 +0800
Change: 2020-05-07 13:44:10.679777123 +0800
 Birth: -
[root@localhost ~]# touch -d "2020-05-07 08:30:30" test3.txt
[root@localhost ~]# stat test3.txt
  File: 'test3.txt'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fd00h/64768d    Inode: 70922637    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2020-05-07 08:30:30.000000000 +0800
Modify: 2020-05-07 08:30:30.000000000 +0800
Change: 2020-05-07 13:44:48.999436890 +0800
 Birth: -
mkdir 命令

mkdir (Make Directory)命令用于创建空白的目录,格式为mkdir [选项] 目录-p参数用来创建多层目录层级。

[root@localhost ~]# mkdir -p test/a
[root@localhost ~]# ll test
total 0
drwxr-xr-x. 2 root root 6 May  7 13:47 a
cp 命令

cp (Copy)命令用于复制文件或目录,格式为cp [选项] 源文件 目标文件

参数作用
-p保留原始文件的属性
-d若对象为“链接文件”,则保留该“链接文件”的属性
-r递归持续复制(用于目录)
-i若目标文件存在则询问是否覆盖
-a相当于-pdr(p、d、r为上述参数)
[root@localhost ~]# cp -a test test2
[root@localhost ~]# ll test2
total 0
drwxr-xr-x. 2 root root 6 May  7 13:47 a
mv 命令

mv (Move)命令用于剪切文件或将文件重命名,格式为mv [选项] 源文件 [目标路径|目标文件名]

[root@localhost ~]# mv test.txt test1.txt
[root@localhost ~]# ll *.txt
-rw-r--r--. 1 root root 12 May  7 13:16 test1.txt
-rw-r--r--. 1 root root 12 May  7 13:41 test2.txt
-rw-r--r--. 1 root root  0 May  7 08:30 test3.txt
rm 命令

rm (Remove)命令用于删除文件或目录,格式为rm [选项] 文件-r代表删除多层级目录和文件,-f代表强制删除。企业关键系统一般会给rm加入执行密码,防止删除造成不可挽回的损失。

[root@localhost ~]# rm -rf test test2
[root@localhost ~]# ll test
ls: cannot access test: No such file or directory
[root@localhost ~]# ll test2
ls: cannot access test2: No such file or directory
dd 命令

dd命令用于按照指定大小和个数的数据块来复制文件或转换文件,格式为dd [参数]

参数作用
if输入的文件名称
of输出的文件名称
bs设置每个“块”的大小
count设置要复制“块”的个数
[root@localhost ~]# dd if=/dev/zero of=/root/300m_file count=1 bs=300M
1+0 records in
1+0 records out
314572800 bytes (315 MB) copied, 0.340217 s, 925 MB/s
[root@localhost ~]# du -sh /root/300m_file
300M    /root/300m_file
[root@localhost ~]#
file 命令

file命令用于查看文件的类型,格式为file 文件名

[root@localhost ~]# file test1.txt
test1.txt: ASCII text
tar 命令

tar命令用于对文件进行打包压缩或解压,格式为tar [选项] [文件]

参数作用
-c创建压缩文件
-x解开压缩文件
-t查看压缩包内有哪些文件
-z用Gzip压缩或解压
-j用bzip2压缩或解压
-v显示压缩或解压的过程
-f目标文件名
-p保留原始的权限与属性
-P使用绝对路径来压缩
-C指定解压到的目录
[root@localhost ~]# ll test/
total 8
-rw-r--r--. 1 root root 12 May  7 13:16 test1.txt
-rw-r--r--. 1 root root 12 May  7 13:41 test2.txt
-rw-r--r--. 1 root root  0 May  7 08:30 test3.txt
[root@localhost ~]# tar czvf test.tar.gz test/
test/
test/test1.txt
test/test2.txt
test/test3.txt
[root@localhost ~]# mkdir /root/test2
[root@localhost ~]# tar tzvf test.tar.gz
drwxr-xr-x root/root         0 2020-05-07 14:25 test/
-rw-r--r-- root/root        12 2020-05-07 13:16 test/test1.txt
-rw-r--r-- root/root        12 2020-05-07 13:41 test/test2.txt
-rw-r--r-- root/root         0 2020-05-07 08:30 test/test3.txt
[root@localhost ~]# tar xzvf test.tar.gz -C test2/
test/
test/test1.txt
test/test2.txt
test/test3.txt
[root@localhost ~]# ll test2/
total 0
drwxr-xr-x. 2 root root 54 May  7 14:25 test
[root@localhost ~]# ll test2/test/
total 8
-rw-r--r--. 1 root root 12 May  7 13:16 test1.txt
-rw-r--r--. 1 root root 12 May  7 13:41 test2.txt
-rw-r--r--. 1 root root  0 May  7 08:30 test3.txt
grep 命令

grep命令用于在文本中执行关键词搜索,并显示匹配的结果,格式为grep [选项] [文件]

参数作用
-b将可执行文件(binary)当作文本文件(text)来搜索
-c仅显示找到的行数
-i忽略大小写
-n显示行号
-v反向选择——仅列出没有“关键词”的行。
[root@localhost ~]# grep /sbin/nologin /etc/passwd
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
unbound:x:998:997:Unbound DNS resolver:/etc/unbound:/sbin/nologin
colord:x:997:996:User for colord:/var/lib/colord:/sbin/nologin
usbmuxd:x:113:113:usbmuxd user:/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
avahi-autoipd:x:170:170:Avahi IPv4LL Stack:/var/lib/avahi-autoipd:/sbin/nologin
libstoragemgmt:x:996:994:daemon account for libstoragemgmt:/var/run/lsm:/sbin/nologin
saslauth:x:995:76:"Saslauthd user":/run/saslauthd:/sbin/nologin
qemu:x:107:107:qemu user:/:/sbin/nologin
rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin
radvd:x:75:75:radvd user:/:/sbin/nologin
ntp:x:38:38::/etc/ntp:/sbin/nologin
chrony:x:994:993::/var/lib/chrony:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
find 命令

find命令用于按照指定条件来查找文件,格式为find [查找路径] 寻找条件 操作

参数作用
-name匹配名称
-perm匹配权限(mode为完全匹配,-mode为包含即可)
-user匹配所有者
-group匹配所有组
-mtime -n +n匹配修改内容的时间(-n指n天以内,+n指n天以前)
-atime -n +n匹配访问文件的时间(-n指n天以内,+n指n天以前)
-ctime -n +n匹配修改文件权限的时间(-n指n天以内,+n指n天以前)
-nouser匹配无所有者的文件
-nogroup匹配无所有组的文件
-newer f1 !f2匹配比文件f1新但比f2旧的文件
–type b/d/c/p/l/f匹配文件类型(后面的字幕字母依次表示块设备、目录、字符设备、管道、链接文件、文本文件)
-size匹配文件的大小(+50KB为查找超过50KB的文件,而-50KB为查找小于50KB的文件)
-prune忽略某个目录
-exec …… {}\;后面可跟用于进一步处理搜索结果的命令
[root@localhost ~]# find /etc -name "host*"
/etc/avahi/hosts
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/selinux/targeted/modules/active/modules/hostname.pp
/etc/hostname
[root@localhost ~]# mkdir find-results
[root@localhost ~]# find /etc/ -name "host*" -exec cp -a {} /root/find-results/ \;
[root@localhost ~]# ll find-results/
total 32
-rw-r--r--. 1 root root    9 Jun  7  2013 host.conf
-rw-r--r--. 1 root root   22 May  4 22:42 hostname
-rw-r--r--. 1 root root 8944 Apr  8  2014 hostname.pp
-rw-r--r--. 1 root root  158 Jun  7  2013 hosts
-rw-r--r--. 1 root root  370 Jun  7  2013 hosts.allow
-rw-r--r--. 1 root root  460 Jun  7  2013 hosts.deny
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值