2. 文件及目录管理
目录
文件管理不外乎文件或目录的创建、删除、查询、移动,有mkdir/rm/mv
文件查询是重点,用find来进行查询;find的参数丰富,也非常强大;
查看文件内容是个大的话题,文本的处理有太多的工具供我们使用,在本章中只是点到即止,后面会有专门的一章来介绍文本的处理工具;
有时候,需要给文件创建一个别名,我们需要用到ln,使用这个别名和使用原文件是相同的效果;
2.1. 创建和删除
- 创建:mkdir
- 删除:rm
- 删除非空目录:rm -rf file目录
- 删除日志 rm *log (等价: $find ./ -name “*log” -exec rm {} ;)
- 移动:mv
- 复制:cp (复制目录:cp -r )
查看当前目录下文件个数:
$find ./ | wc -l
复制目录:
$cp -r source_dir dest_dir
2.2. 目录切换
2.2. 目录切换
- 找到文件/目录位置:cd
- 切换到上一个工作目录: cd -
- 切换到home目录: cd or cd ~
- 显示当前路径: pwd
- 更改当前工作路径为path: $cd path
2.3. 列出目录项
- 显示当前目录下的文件 ls
- 按时间排序,以列表的方式显示目录项 ls -lrt
以上这个命令用到的频率如此之高,以至于我们需要为它建立一个快捷命令方式:
在.bashrc 中设置命令别名:
alias lsl='ls -lrt'
alias lm='ls -al|more'
这样,使用lsl,就可以显示目录中的文件按照修改时间排序;以列表方式显示;
-
给每项文件前面增加一个id编号(看上去更加整洁):
>ls | cat -n
1 a 2 a.out 3 app 4 b 5 bin 6 config
注:.bashrc 在/home/你的用户名/ 文件夹下,以隐藏文件的方式存储;可使用 ls -a 查看;
root@bae226a5b14a:/# find ./ | wc -l 25917 root@bae226a5b14a:/# cd bin root@bae226a5b14a:/bin# ls bash bzip2 dash egrep kill mount rm sync wdctl zgrep bunzip2 bzip2recover date false ln mountpoint rmdir tar which zless bzcat bzless dd fgrep login mv run-parts tempfile ypdomainname zmore bzcmp bzmore df findmnt ls nisdomainname sed touch zcat znew bzdiff cat dir grep lsblk pidof sh true zcmp bzegrep chgrp dmesg gunzip mkdir ps sh.distrib umount zdiff bzexe chmod dnsdomainname gzexe mknod pwd sleep uname zegrep bzfgrep chown domainname gzip mktemp rbash stty uncompress zfgrep bzgrep cp echo hostname more readlink su vdir zforce root@bae226a5b14a:/bin# cd bash bash: cd: bash: Not a directory root@bae226a5b14a:/bin# cd pidof bash: cd: pidof: Not a directory root@bae226a5b14a:/bin# cd root@bae226a5b14a:~# cd bin bash: cd: bin: No such file or directory root@bae226a5b14a:~# cd .. root@bae226a5b14a:/# pwd / root@bae226a5b14a:/# cd bin/ root@bae226a5b14a:/bin# ls bash bzip2 dash egrep kill mount rm sync wdctl zgrep bunzip2 bzip2recover date false ln mountpoint rmdir tar which zless bzcat bzless dd fgrep login mv run-parts tempfile ypdomainname zmore bzcmp bzmore df findmnt ls nisdomainname sed touch zcat znew bzdiff cat dir grep lsblk pidof sh true zcmp bzegrep chgrp dmesg gunzip mkdir ps sh.distrib umount zdiff bzexe chmod dnsdomainname gzexe mknod pwd sleep uname zegrep bzfgrep chown domainname gzip mktemp rbash stty uncompress zfgrep bzgrep cp echo hostname more readlink su vdir zforce root@bae226a5b14a:/bin# pwd /bin root@bae226a5b14a:/bin# ls -lrt total 4940 -rwxr-xr-x 1 root root 5047 Apr 28 2017 znew -rwxr-xr-x 1 root root 1910 Apr 28 2017 zmore -rwxr-xr-x 1 root root 2037 Apr 28 2017 zless -rwxr-xr-x 1 root root 5938 Apr 28 2017 zgrep -rwxr-xr-x 1 root root 2131 Apr 28 2017 zforce -rwxr-xr-x 1 root root 140 Apr 28 2017 zfgrep -rwxr-xr-x 1 root root 140 Apr 28 2017 zegrep -rwxr-xr-x 1 root root 5764 Apr 28 2017 zdiff -rwxr-xr-x 1 root root 1777 Apr 28 2017 zcmp -rwxr-xr-x 1 root root 1937 Apr 28 2017 zcat -rwxr-xr-x 2 root root 2301 Apr 28 2017 uncompress -rwxr-xr-x 1 root root 101560 Apr 28 2017 gzip -rwxr-xr-x 1 root root 5927 Apr 28 2017 gzexe -rwxr-xr-x 2 root root 2301 Apr 28 2017 gunzip lrwxrwxrwx 1 root root 14 Nov 1 2017 pidof -> /sbin/killall5 -rwxr-xr-x 1 root root 946 Dec 30 2017 which -rwxr-xr-x 1 root root 10104 Dec 30 2017 tempfile -rwxr-xr-x 1 root root 18760 Dec 30 2017 run-parts -rwxr-xr-x 1 root root 133792 Jan 18 2018 vdir -rwxr-xr-x 1 root root 35032 Jan 18 2018 uname -rwxr-xr-x 1 root root 30904 Jan 18 2018 true -rwxr-xr-x 1 root root 88280 Jan 18 2018 touch -rwxr-xr-x 1 root root 35000 Jan 18 2018 sync -rwxr-xr-x 1 root root 75992 Jan 18 2018 stty -rwxr-xr-x 1 root root 35000 Jan 18 2018 sleep -rwxr-xr-x 1 root root 43192 Jan 18 2018 rmdir -rwxr-xr-x 1 root root 63704 Jan 18 2018 rm -rwxr-xr-x 1 root root 43192 Jan 18 2018 readlink -rwxr-xr-x 1 root root 35000 Jan 18 2018 pwd -rwxr-xr-x 1 root root 137440 Jan 18 2018 mv -rwxr-xr-x 1 root root 43192 Jan 18 2018 mktemp -rwxr-xr-x 1 root root 67768 Jan 18 2018 mknod -rwxr-xr-x 1 root root 80056 Jan 18 2018 mkdir -rwxr-xr-x 1 root root 133792 Jan 18 2018 ls -rwxr-xr-x 1 root root 67808 Jan 18 2018 ln -rwxr-xr-x 1 root root 30904 Jan 18 2018 false -rwxr-xr-x 1 root root 35000 Jan 18 2018 echo -rwxr-xr-x 1 root root 133792 Jan 18 2018 dir -rwxr-xr-x 1 root root 84776 Jan 18 2018 df -rwxr-xr-x 1 root root 76000 Jan 18 2018 dd -rwxr-xr-x 1 root root 100568 Jan 18 2018 date -rwxr-xr-x 1 root root 141528 Jan 18 2018 cp -rwxr-xr-x 1 root root 67768 Jan 18 2018 chown -rwxr-xr-x 1 root root 59608 Jan 18 2018 chmod -rwxr-xr-x 1 root root 63672 Jan 18 2018 chgrp -rwxr-xr-x 1 root root 35064 Jan 18 2018 cat -rwxr-xr-x 1 root root 121432 Jan 25 2018 dash -rwxr-xr-x 1 root root 109000 Jan 30 2018 sed lrwxrwxrwx 1 root root 8 Jan 31 2018 ypdomainname -> hostname lrwxrwxrwx 1 root root 8 Jan 31 2018 nisdomainname -> hostname -rwxr-xr-x 1 root root 18504 Jan 31 2018 hostname lrwxrwxrwx 1 root root 8 Jan 31 2018 domainname -> hostname lrwxrwxrwx 1 root root 8 Jan 31 2018 dnsdomainname -> hostname -rwxr-xr-x 1 root root 423312 Jan 21 2019 tar -rwsr-xr-x 1 root root 44664 Mar 22 2019 su -rwxr-xr-x 1 root root 52664 Mar 22 2019 login lrwxrwxrwx 1 root root 4 Jun 6 2019 rbash -> bash -rwxr-xr-x 1 root root 1113504 Jun 6 2019 bash -rwxr-xr-x 1 root root 1297 Jul 4 2019 bzmore lrwxrwxrwx 1 root root 6 Jul 4 2019 bzless -> bzmore -rwxr-xr-x 1 root root 14328 Jul 4 2019 bzip2recover -rwxr-xr-x 3 root root 34888 Jul 4 2019 bzip2 -rwxr-xr-x 1 root root 3642 Jul 4 2019 bzgrep lrwxrwxrwx 1 root root 6 Jul 4 2019 bzfgrep -> bzgrep -rwxr-xr-x 1 root root 4877 Jul 4 2019 bzexe lrwxrwxrwx 1 root root 6 Jul 4 2019 bzegrep -> bzgrep -rwxr-xr-x 1 root root 2140 Jul 4 2019 bzdiff lrwxrwxrwx 1 root root 6 Jul 4 2019 bzcmp -> bzdiff -rwxr-xr-x 3 root root 34888 Jul 4 2019 bzcat -rwxr-xr-x 3 root root 34888 Jul 4 2019 bunzip2 -rwxr-xr-x 1 root root 133432 Aug 9 2019 ps -rwxr-xr-x 1 root root 26704 Aug 9 2019 kill -rwxr-xr-x 1 root root 219456 Sep 18 2019 grep -rwxr-xr-x 1 root root 28 Sep 18 2019 fgrep -rwxr-xr-x 1 root root 28 Sep 18 2019 egrep -rwxr-xr-x 1 root root 30800 Jan 8 18:31 wdctl -rwsr-xr-x 1 root root 26696 Jan 8 18:31 umount -rwxr-xr-x 1 root root 14408 Jan 8 18:31 mountpoint -rwsr-xr-x 1 root root 43088 Jan 8 18:31 mount -rwxr-xr-x 1 root root 38952 Jan 8 18:31 more -rwxr-xr-x 1 root root 84048 Jan 8 18:31 lsblk -rwxr-xr-x 1 root root 64784 Jan 8 18:31 findmnt -rwxr-xr-x 1 root root 72000 Jan 8 18:31 dmesg lrwxrwxrwx 1 root root 4 Mar 11 21:03 sh.distrib -> dash lrwxrwxrwx 1 root root 4 Mar 11 21:04 sh -> dash root@bae226a5b14a:/bin# ls | cat -n 1 bash 2 bunzip2 3 bzcat 4 bzcmp 5 bzdiff 6 bzegrep 7 bzexe 8 bzfgrep 9 bzgrep 10 bzip2 11 bzip2recover 12 bzless 13 bzmore 14 cat 15 chgrp 16 chmod 17 chown 18 cp 19 dash 20 date 21 dd 22 df 23 dir 24 dmesg 25 dnsdomainname 26 domainname 27 echo 28 egrep 29 false 30 fgrep 31 findmnt 32 grep 33 gunzip 34 gzexe 35 gzip 36 hostname 37 kill 38 ln 39 login 40 ls 41 lsblk 42 mkdir 43 mknod 44 mktemp 45 more 46 mount 47 mountpoint 48 mv 49 nisdomainname 50 pidof 51 ps 52 pwd 53 rbash 54 readlink 55 rm 56 rmdir 57 run-parts 58 sed 59 sh 60 sh.distrib 61 sleep 62 stty 63 su 64 sync 65 tar 66 tempfile 67 touch 68 true 69 umount 70 uname 71 uncompress 72 vdir 73 wdctl 74 which 75 ypdomainname 76 zcat 77 zcmp 78 zdiff 79 zegrep 80 zfgrep 81 zforce 82 zgrep 83 zless 84 zmore 85 znew root@bae226a5b14a:/bin# ls-a bash: ls-a: command not found root@bae226a5b14a:/bin# ls -a . bzfgrep chown domainname gzip mktemp rbash stty uncompress zfgrep .. bzgrep cp echo hostname more readlink su vdir zforce bash bzip2 dash egrep kill mount rm sync wdctl zgrep bunzip2 bzip2recover date false ln mountpoint rmdir tar which zless bzcat bzless dd fgrep login mv run-parts tempfile ypdomainname zmore bzcmp bzmore df findmnt ls nisdomainname sed touch zcat znew bzdiff cat dir grep lsblk pidof sh true zcmp bzegrep chgrp dmesg gunzip mkdir ps sh.distrib umount zdiff bzexe chmod dnsdomainname gzexe mknod pwd sleep uname zegrep root@bae226a5b14a:/bin#
2.4. 查找目录及文件 find/locate
搜寻文件或目录:
$find ./ -name "core*" | xargs file
查找目标文件夹中是否有obj文件:
$find ./ -name '*.o'
递归当前目录及子目录删除所有.o文件:
$find ./ -name "*.o" -exec rm {} ;
find是实时查找,如果需要更快的查询,可试试locate;locate会为文件系统建立索引数据库,如果有文件更新,需要定期执行更新命令来更新索引库:
$locate string
寻找包含有string的路径:
$updatedb
与find不同,locate并不是实时查找。你需要更新数据库,以获得最新的文件索引信息。
root@bae226a5b14a:/bin# find ./ -name "core*" | xargs file Usage: file [-bcCdEhikLlNnprsSvzZ0] [--apple] [--extension] [--mime-encoding] [--mime-type] [-e <testname>] [-F <separator>] [-f <namefile>] [-m <magicfiles>] [-P <parameter=value>] <file> ... file -C [-m <magicfiles>] file [--help] root@bae226a5b14a:/bin# cd .. root@bae226a5b14a:/# find ./ -name "core*" | xargs file ./sys/devices/system/cpu/cpu5/topology/core_siblings: ASCII text ./sys/devices/system/cpu/cpu5/topology/core_siblings_list: ASCII text ./sys/devices/system/cpu/cpu5/topology/core_id: ASCII text ./sys/devices/system/cpu/cpu3/topology/core_siblings: ASCII text ./sys/devices/system/cpu/cpu3/topology/core_siblings_list: ASCII text ./sys/devices/system/cpu/cpu3/topology/core_id: ASCII text ./sys/devices/system/cpu/cpu1/topology/core_siblings: ASCII text ./sys/devices/system/cpu/cpu1/topology/core_siblings_list: ASCII text ./sys/devices/system/cpu/cpu1/topology/core_id: ASCII text ./sys/devices/system/cpu/cpu4/topology/core_siblings: ASCII text ./sys/devices/system/cpu/cpu4/topology/core_siblings_list: ASCII text ./sys/devices/system/cpu/cpu4/topology/core_id: ASCII text ./sys/devices/system/cpu/cpu2/topology/core_siblings: ASCII text ./sys/devices/system/cpu/cpu2/topology/core_siblings_list: ASCII text ./sys/devices/system/cpu/cpu2/topology/core_id: ASCII text ./sys/devices/system/cpu/cpu0/topology/core_siblings: ASCII text ./sys/devices/system/cpu/cpu0/topology/core_siblings_list: ASCII text ./sys/devices/system/cpu/cpu0/topology/core_id: ASCII text ./sys/module/bpfilter/coresize: ASCII text ./sys/module/vsock/coresize: ASCII text ./sys/module/xfrm_algo/coresize: ASCII text ./sys/module/hv_sock/coresize: ASCII text ./sys/module/xfrm_user/coresize: ASCII text ./usr/lib/x86_64-linux-gnu/coreutils: directory ./usr/share/man/man5/core.5.gz: gzip compressed data, max compression, from Unix, original size modulo 2^32 20005 ./usr/share/info/coreutils.info.gz: gzip compressed data, max compression, from Unix, original size modulo 2^32 897636 ./usr/share/doc/coreutils: directory ./proc/sys/kernel/core_pattern: empty ./proc/sys/kernel/core_pipe_limit: empty ./proc/sys/kernel/core_uses_pid: empty ./proc/sys/net/core: directory ./proc/1/coredump_filter: empty ./proc/14683/coredump_filter: cannot open `./proc/14683/coredump_filter' (No such file or directory) ./proc/14684/coredump_filter: empty ./var/lib/dpkg/info/coreutils.md5sums: ASCII text ./var/lib/dpkg/info/coreutils.postrm: POSIX shell script, ASCII text executable ./var/lib/dpkg/info/coreutils.list: ASCII text ./var/lib/dpkg/info/coreutils.postinst: POSIX shell script, ASCII text executable ./dev/core: symbolic link to /proc/kcore root@bae226a5b14a:/# find ./ -name '*.o' root@bae226a5b14a:/# locate string bash: locate: command not found root@bae226a5b14a:/# apt-get install locate Reading package lists... Done Building dependency tree Reading state information... Done The following NEW packages will be installed: locate 0 upgraded, 1 newly installed, 0 to remove and 81 not upgraded. Need to get 85.3 kB of archives. After this operation, 265 kB of additional disk space will be used. Get:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu focal/universe amd64 locate amd64 4.7.0-1ubuntu1 [85.3 kB] Fetched 85.3 kB in 1s (71.5 kB/s) debconf: delaying package configuration, since apt-utils is not installed Selecting previously unselected package locate. (Reading database ... 12934 files and directories currently installed.) Preparing to unpack .../locate_4.7.0-1ubuntu1_amd64.deb ... Unpacking locate (4.7.0-1ubuntu1) ... Setting up locate (4.7.0-1ubuntu1) ... Processing triggers for man-db (2.9.1-1) ... root@bae226a5b14a:/# locate string root@bae226a5b14a:/# locate obj root@bae226a5b14a:/# locate bin root@bae226a5b14a:/# updatedb root@bae226a5b14a:/# locate bin /bin /bin/bash /bin/bunzip2 /bin/bzcat /bin/bzcmp /bin/bzdiff /bin/bzegrep /bin/bzexe /bin/bzfgrep /bin/bzgrep /bin/bzip2 /bin/bzip2recover /bin/bzless /bin/bzmore /bin/cat /bin/chgrp /bin/chmod /bin/chown /bin/cp /bin/dash /bin/date /bin/dd /bin/df /bin/dir /bin/dmesg /bin/dnsdomainname /bin/domainname /bin/echo /bin/egrep /bin/false /bin/fgrep /bin/findmnt /bin/grep /bin/gunzip /bin/gzexe /bin/gzip /bin/hostname /bin/kill /bin/ln /bin/login /bin/ls /bin/lsblk /bin/mkdir /bin/mknod /bin/mktemp /bin/more /bin/mount /bin/mountpoint /bin/mv /bin/nisdomainname /bin/pidof /bin/ps /bin/pwd /bin/rbash /bin/readlink /bin/rm /bin/rmdir /bin/run-parts /bin/sed /bin/sh /bin/sh.distrib /bin/sleep /bin/stty /bin/su /bin/sync /bin/tar /bin/tempfile /bin/touch /bin/true /bin/umount /bin/uname /bin/uncompress /bin/vdir /bin/wdctl /bin/which /bin/ypdomainname /bin/zcat /bin/zcmp /bin/zdiff /bin/zegrep /bin/zfgrep /bin/zforce /bin/zgrep /bin/zless /bin/zmore /bin/znew /etc/apparmor.d/local/usr.bin.man /etc/apparmor.d/usr.bin.man /etc/bindresvport.blacklist /sbin /sbin/agetty /sbin/badblocks /sbin/blkdiscard /sbin/blkid /sbin/blockdev /sbin/cfdisk /sbin/chcpu /sbin/ctrlaltdel /sbin/debugfs /sbin/dumpe2fs /sbin/e2fsck /sbin/e2image /sbin/e2label /sbin/e2undo /sbin/fdisk /sbin/findfs /sbin/fsck /sbin/fsck.cramfs /sbin/fsck.ext2 /sbin/fsck.ext3 /sbin/fsck.ext4 /sbin/fsck.minix /sbin/fsfreeze /sbin/fstab-decode /sbin/fstrim /sbin/getty /sbin/hwclock /sbin/initctl /sbin/installkernel /sbin/isosize /sbin/killall5 /sbin/ldconfig /sbin/ldconfig.real /sbin/logsave /sbin/losetup /sbin/mke2fs /sbin/mkfs /sbin/mkfs.bfs /sbin/mkfs.cramfs /sbin/mkfs.ext2 /sbin/mkfs.ext3 /sbin/mkfs.ext4 /sbin/mkfs.minix /sbin/mkhomedir_helper /sbin/mkswap /sbin/pam_extrausers_chkpwd /sbin/pam_extrausers_update /sbin/pam_tally /sbin/pam_tally2 /sbin/pivot_root /sbin/raw /sbin/resize2fs /sbin/runuser /sbin/sfdisk /sbin/shadowconfig /sbin/start-stop-daemon /sbin/sulogin /sbin/swaplabel /sbin/swapoff /sbin/swapon /sbin/switch_root /sbin/sysctl /sbin/tune2fs /sbin/unix_chkpwd /sbin/unix_update /sbin/wipefs /sbin/zramctl /usr/bin /usr/bin/[ /usr/bin/addpart /usr/bin/apropos /usr/bin/apt /usr/bin/apt-cache /usr/bin/apt-cdrom /usr/bin/apt-config /usr/bin/apt-get /usr/bin/apt-key /usr/bin/apt-mark /usr/bin/arch /usr/bin/awk /usr/bin/b2sum /usr/bin/base32 /usr/bin/base64 /usr/bin/basename /usr/bin/bashbug /usr/bin/bsd-from /usr/bin/bsd-write /usr/bin/cal /usr/bin/calendar /usr/bin/captoinfo /usr/bin/catchsegv /usr/bin/catman /usr/bin/cautious-launcher /usr/bin/chage /usr/bin/chattr /usr/bin/chcon /usr/bin/chfn /usr/bin/chrt /usr/bin/chsh /usr/bin/cksum /usr/bin/clear /usr/bin/clear_console /usr/bin/cmp /usr/bin/col /usr/bin/colcrt /usr/bin/colrm /usr/bin/column /usr/bin/comm /usr/bin/compose /usr/bin/csplit /usr/bin/cut /usr/bin/deb-systemd-helper /usr/bin/deb-systemd-invoke /usr/bin/debconf /usr/bin/debconf-apt-progress /usr/bin/debconf-communicate /usr/bin/debconf-copydb /usr/bin/debconf-escape /usr/bin/debconf-set-selections /usr/bin/debconf-show /usr/bin/delpart /usr/bin/diff /usr/bin/diff3 /usr/bin/dircolors /usr/bin/dirname /usr/bin/dpkg /usr/bin/dpkg-deb /usr/bin/dpkg-divert /usr/bin/dpkg-maintscript-helper /usr/bin/dpkg-query /usr/bin/dpkg-split /usr/bin/dpkg-statoverride /usr/bin/dpkg-trigger /usr/bin/du /usr/bin/edit /usr/bin/editor /usr/bin/env /usr/bin/eqn /usr/bin/ex /usr/bin/expand /usr/bin/expiry /usr/bin/expr /usr/bin/factor /usr/bin/faillog /usr/bin/fallocate /usr/bin/file /usr/bin/find /usr/bin/flock /usr/bin/fmt /usr/bin/fold /usr/bin/free /usr/bin/from /usr/bin/geqn /usr/bin/getconf /usr/bin/getent /usr/bin/getopt /usr/bin/ginstall-info /usr/bin/gpasswd /usr/bin/gpgv /usr/bin/gpic /usr/bin/groff /usr/bin/grog /usr/bin/grops /usr/bin/grotty /usr/bin/groups /usr/bin/gtbl /usr/bin/hd /usr/bin/head /usr/bin/helpztags /usr/bin/hexdump /usr/bin/hostid /usr/bin/i386 /usr/bin/iconv /usr/bin/id /usr/bin/info /usr/bin/infobrowser /usr/bin/infocmp /usr/bin/infotocap /usr/bin/install /usr/bin/install-info /usr/bin/ionice /usr/bin/ipcmk /usr/bin/ipcrm /usr/bin/ipcs /usr/bin/ischroot /usr/bin/join /usr/bin/last /usr/bin/lastb /usr/bin/lastlog /usr/bin/ldd /usr/bin/lexgrog /usr/bin/link /usr/bin/linux32 /usr/bin/linux64 /usr/bin/locale /usr/bin/locale-check /usr/bin/localedef /usr/bin/locate /usr/bin/locate.findutils /usr/bin/logger /usr/bin/logname /usr/bin/look /usr/bin/lorder /usr/bin/lsattr /usr/bin/lscpu /usr/bin/lsdev /usr/bin/lsipc /usr/bin/lslocks /usr/bin/lslogins /usr/bin/lsmem /usr/bin/lsns /usr/bin/lzcat /usr/bin/lzcmp /usr/bin/lzdiff /usr/bin/lzegrep /usr/bin/lzfgrep /usr/bin/lzgrep /usr/bin/lzless /usr/bin/lzma /usr/bin/lzmainfo /usr/bin/lzmore /usr/bin/man /usr/bin/man-recode /usr/bin/mandb /usr/bin/manpath /usr/bin/mawk /usr/bin/mcookie /usr/bin/md5sum /usr/bin/md5sum.textutils /usr/bin/mesg /usr/bin/mkfifo /usr/bin/namei /usr/bin/nawk /usr/bin/ncal /usr/bin/neqn /usr/bin/newgrp /usr/bin/nice /usr/bin/nl /usr/bin/nohup /usr/bin/nproc /usr/bin/nroff /usr/bin/nsenter /usr/bin/numfmt /usr/bin/od /usr/bin/pager /usr/bin/partx /usr/bin/passwd /usr/bin/paste /usr/bin/pathchk /usr/bin/perl /usr/bin/perl5.26.1 /usr/bin/pgrep /usr/bin/pic /usr/bin/pinky /usr/bin/pkill /usr/bin/pldd /usr/bin/pmap /usr/bin/pr /usr/bin/preconv /usr/bin/print /usr/bin/printenv /usr/bin/printerbanner /usr/bin/printf /usr/bin/prlimit /usr/bin/procinfo /usr/bin/ptx /usr/bin/pwdx /usr/bin/realpath /usr/bin/rename.ul /usr/bin/renice /usr/bin/reset /usr/bin/resizepart /usr/bin/rev /usr/bin/rgrep /usr/bin/run-mailcap /usr/bin/runcon /usr/bin/rview /usr/bin/rvim /usr/bin/savelog /usr/bin/script /usr/bin/scriptreplay /usr/bin/sdiff /usr/bin/see /usr/bin/select-editor /usr/bin/sensible-browser /usr/bin/sensible-editor /usr/bin/sensible-pager /usr/bin/seq /usr/bin/setarch /usr/bin/setsid /usr/bin/setterm /usr/bin/sg /usr/bin/sha1sum /usr/bin/sha224sum /usr/bin/sha256sum /usr/bin/sha384sum /usr/bin/sha512sum /usr/bin/shred /usr/bin/shuf /usr/bin/skill /usr/bin/slabtop /usr/bin/snice /usr/bin/socklist /usr/bin/soelim /usr/bin/sort /usr/bin/split /usr/bin/stat /usr/bin/stdbuf /usr/bin/sum /usr/bin/tabs /usr/bin/tac /usr/bin/tail /usr/bin/taskset /usr/bin/tbl /usr/bin/tee /usr/bin/test /usr/bin/tic /usr/bin/timeout /usr/bin/tload /usr/bin/toe /usr/bin/top /usr/bin/touch /usr/bin/tput /usr/bin/tr /usr/bin/troff /usr/bin/truncate /usr/bin/tset /usr/bin/tsort /usr/bin/tty /usr/bin/tzselect /usr/bin/ul /usr/bin/unexpand /usr/bin/uniq /usr/bin/unlink /usr/bin/unlzma /usr/bin/unshare /usr/bin/unxz /usr/bin/update-alternatives /usr/bin/updatedb /usr/bin/updatedb.findutils /usr/bin/uptime /usr/bin/users /usr/bin/utmpdump /usr/bin/vi /usr/bin/view /usr/bin/vim /usr/bin/vim.basic /usr/bin/vimdiff /usr/bin/vimtutor /usr/bin/vmstat /usr/bin/w /usr/bin/w.procps /usr/bin/wall /usr/bin/watch /usr/bin/wc /usr/bin/whatis /usr/bin/whereis /usr/bin/which /usr/bin/who /usr/bin/whoami /usr/bin/write /usr/bin/x86_64 /usr/bin/xargs /usr/bin/xxd /usr/bin/xz /usr/bin/xzcat /usr/bin/xzcmp /usr/bin/xzdiff /usr/bin/xzegrep /usr/bin/xzfgrep /usr/bin/xzgrep /usr/bin/xzless /usr/bin/xzmore /usr/bin/yes /usr/bin/zdump /usr/lib/man-db/globbing /usr/lib/python3.8/binhex.py /usr/lib/python3.8/lib-dynload/_xxsubinterpreters.cpython-38-x86_64-linux-gnu.so /usr/local/bin /usr/local/sbin /usr/local/sbin/unminimize /usr/sbin /usr/sbin/accessdb /usr/sbin/add-shell /usr/sbin/addgroup /usr/sbin/adduser /usr/sbin/chgpasswd /usr/sbin/chmem /usr/sbin/chpasswd /usr/sbin/chroot /usr/sbin/cpgr /usr/sbin/cppw /usr/sbin/delgroup /usr/sbin/deluser /usr/sbin/dpkg-preconfigure /usr/sbin/dpkg-reconfigure /usr/sbin/e2freefrag /usr/sbin/e4crypt /usr/sbin/e4defrag /usr/sbin/fdformat /usr/sbin/filefrag /usr/sbin/groupadd /usr/sbin/groupdel /usr/sbin/groupmems /usr/sbin/groupmod /usr/sbin/grpck /usr/sbin/grpconv /usr/sbin/grpunconv /usr/sbin/iconvconfig /usr/sbin/invoke-rc.d /usr/sbin/ldattach /usr/sbin/mklost+found /usr/sbin/newusers /usr/sbin/nologin /usr/sbin/pam-auth-update /usr/sbin/pam_getenv /usr/sbin/pam_timestamp_check /usr/sbin/policy-rc.d /usr/sbin/pwck /usr/sbin/pwconv /usr/sbin/pwunconv /usr/sbin/readprofile /usr/sbin/remove-shell /usr/sbin/rmt /usr/sbin/rmt-tar /usr/sbin/rtcwake /usr/sbin/service /usr/sbin/tarcat /usr/sbin/update-info-dir /usr/sbin/update-mime /usr/sbin/update-passwd /usr/sbin/update-rc.d /usr/sbin/useradd /usr/sbin/userdel /usr/sbin/usermod /usr/sbin/vigr /usr/sbin/vipw /usr/sbin/zic /usr/share/doc/libc-bin /usr/share/doc/libc-bin/changelog.Debian.gz /usr/share/doc/libc-bin/copyright /usr/share/doc/libpam-modules-bin /usr/share/doc/libpam-modules-bin/NEWS.Debian.gz /usr/share/doc/libpam-modules-bin/changelog.Debian.gz /usr/share/doc/libpam-modules-bin/copyright /usr/share/doc/ncurses-bin /usr/share/doc/ncurses-bin/changelog.Debian.gz /usr/share/doc/ncurses-bin/copyright /usr/share/libc-bin /usr/share/libc-bin/nsswitch.conf /usr/share/lintian/overrides/libc-bin /usr/share/lintian/overrides/libpam-modules-bin /usr/share/man/man2/bind.2.gz /usr/share/man/man2/mbind.2.gz /usr/share/man/man3/bind.3posix.gz /usr/share/man/man3/bindresvport.3.gz /usr/share/man/man5/slabinfo.5.gz /usr/share/man/zh_CN/man1/bind.1.gz /usr/share/man/zh_CN/man1/wbinfo.1.gz /usr/share/man/zh_CN/man2/bind.2.gz /usr/share/man/zh_CN/man3/binary.3tcl.gz /usr/share/man/zh_CN/man3/bindtags.3tk.gz /usr/share/man/zh_CN/man3/bindtextdomain.3.gz /usr/share/man/zh_CN/man5/binfmt.d.5.gz /usr/share/man/zh_TW/man1/bind.1.gz /usr/share/man/zh_TW/man1/wbinfo.1.gz /usr/share/man/zh_TW/man2/bind.2.gz /usr/share/man/zh_TW/man3/binary.3tcl.gz /usr/share/man/zh_TW/man3/bindtags.3tk.gz /usr/share/man/zh_TW/man3/bindtextdomain.3.gz /usr/share/man/zh_TW/man5/binfmt.d.5.gz /usr/share/sensible-utils/bin /usr/share/sensible-utils/bin/gettext /usr/share/vim/vim81/syntax/bindzone.vim /var/lib/apt/lists/mirrors.tuna.tsinghua.edu.cn_ubuntu_dists_focal_main_binary-amd64_Packages.lz4 /var/lib/apt/lists/mirrors.tuna.tsinghua.edu.cn_ubuntu_dists_focal_multiverse_binary-amd64_Packages.lz4 /var/lib/apt/lists/mirrors.tuna.tsinghua.edu.cn_ubuntu_dists_focal_restricted_binary-amd64_Packages.lz4 /var/lib/apt/lists/mirrors.tuna.tsinghua.edu.cn_ubuntu_dists_focal_universe_binary-amd64_Packages.lz4 /var/lib/dpkg/info/libc-bin.conffiles /var/lib/dpkg/info/libc-bin.list /var/lib/dpkg/info/libc-bin.md5sums /var/lib/dpkg/info/libc-bin.postinst /var/lib/dpkg/info/libc-bin.triggers /var/lib/dpkg/info/libpam-modules-bin.list /var/lib/dpkg/info/libpam-modules-bin.md5sums /var/lib/dpkg/info/ncurses-bin.list /var/lib/dpkg/info/ncurses-bin.md5sums root@bae226a5b14a:/# locate string /usr/lib/python3.8/string.py /usr/lib/python3.8/stringprep.py /usr/lib/x86_64-linux-gnu/libunistring.so.2 /usr/lib/x86_64-linux-gnu/libunistring.so.2.1.0 /usr/share/alsa/ucm2/chtnau8824/PIPO-W2S-Defaultstring-CherryTrailCR.conf /usr/share/alsa/ucm2/chtnau8824/cube-i1_TF-Defaultstring-CherryTrailCR.conf /usr/share/alsa/ucm2/chtrt5645/TECLAST-X80Pro-Defaultstring-CherryTrailCR.conf /usr/share/doc/libunistring2 /usr/share/doc/libunistring2/changelog.Debian.gz /usr/share/doc/libunistring2/copyright /usr/share/man/man1/strings.1posix.gz /usr/share/man/man3/argz_stringify.3.gz /usr/share/man/man3/bstring.3.gz /usr/share/man/man3/string.3.gz /usr/share/man/man3/xdr_string.3.gz /usr/share/man/man3/xdr_wrapstring.3.gz /usr/share/man/man7/string.h.7posix.gz /usr/share/man/man7/strings.h.7posix.gz /usr/share/man/zh_CN/man1/strings.1.gz /usr/share/man/zh_CN/man3/string.3tcl.gz /usr/share/man/zh_TW/man1/strings.1.gz /usr/share/man/zh_TW/man3/string.3tcl.gz /var/lib/dpkg/info/libunistring2:amd64.list /var/lib/dpkg/info/libunistring2:amd64.md5sums /var/lib/dpkg/info/libunistring2:amd64.shlibs /var/lib/dpkg/info/libunistring2:amd64.symbols /var/lib/dpkg/info/libunistring2:amd64.triggers root@bae226a5b14a:/#
2.5. 查看文件内容
查看文件:cat vi head tail more
显示时同时显示行号:
$cat -n
按页显示列表内容:
$ls -al | more
只看前10行:
$head - 10 **
显示文件第一行:
$head -1 filename
显示文件倒数第五行:
$tail -5 filename
查看两个文件间的差别:
$diff file1 file2
动态显示文本最新信息:
$tail -f crawler.log
root@bae226a5b14a:/# cat -n 1 2 3 4 5 6 7 8 9 10 11 12 13 ^C root@bae226a5b14a:/# ls -al |more total 88 drwxr-xr-x 1 root root 4096 Apr 9 07:25 . drwxr-xr-x 1 root root 4096 Apr 9 07:25 .. -rwxr-xr-x 1 root root 0 Apr 9 07:25 .dockerenv drwxr-xr-x 2 root root 4096 Mar 11 21:05 bin drwxr-xr-x 2 root root 4096 Apr 24 2018 boot drwxr-xr-x 5 root root 360 Apr 9 07:25 dev drwxr-xr-x 1 root root 4096 Apr 9 08:29 etc drwxr-xr-x 2 root root 4096 Apr 24 2018 home drwxr-xr-x 1 root root 4096 May 23 2017 lib drwxr-xr-x 1 root root 4096 Apr 9 07:24 lib64 drwxr-xr-x 2 root root 4096 Mar 11 21:03 media drwxr-xr-x 2 root root 4096 Mar 11 21:03 mnt drwxr-xr-x 2 root root 4096 Mar 11 21:03 opt dr-xr-xr-x 156 root root 0 Apr 9 07:25 proc drwx------ 1 root root 4096 Apr 9 08:08 root drwxr-xr-x 1 root root 4096 Mar 20 19:20 run drwxr-xr-x 1 root root 4096 Apr 9 07:24 sbin drwxr-xr-x 2 root root 4096 Mar 11 21:03 srv dr-xr-xr-x 13 root root 0 Apr 9 07:25 sys drwxrwxrwt 1 root root 4096 Apr 9 08:45 tmp drwxr-xr-x 1 root root 4096 Mar 11 21:03 usr drwxr-xr-x 1 root root 4096 Mar 11 21:05 var root@bae226a5b14a:/# head - 10 ** ==> standard input <== ^C root@bae226a5b14a:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@bae226a5b14a:/# head -1 tmp/ head: error reading 'tmp/': Is a directory root@bae226a5b14a:/# cd tmp/ root@bae226a5b14a:/tmp# ls root@bae226a5b14a:/tmp# cd .. root@bae226a5b14a:/# cd bin/ root@bae226a5b14a:/bin# ls bash bzip2 dash egrep kill mount rm sync wdctl zgrep bunzip2 bzip2recover date false ln mountpoint rmdir tar which zless bzcat bzless dd fgrep login mv run-parts tempfile ypdomainname zmore bzcmp bzmore df findmnt ls nisdomainname sed touch zcat znew bzdiff cat dir grep lsblk pidof sh true zcmp bzegrep chgrp dmesg gunzip mkdir ps sh.distrib umount zdiff bzexe chmod dnsdomainname gzexe mknod pwd sleep uname zegrep bzfgrep chown domainname gzip mktemp rbash stty uncompress zfgrep bzgrep cp echo hostname more readlink su vdir zforce root@bae226a5b14a:/bin# cd .. root@bae226a5b14a:/# echo hello>1.test root@bae226a5b14a:/# ls 1.test bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@bae226a5b14a:/# head -1 1.test hello root@bae226a5b14a:/# tail -5 1.test hello root@bae226a5b14a:/# echo hello word>.test root@bae226a5b14a:/# echo hello world>2.test root@bae226a5b14a:/# tail -5 2.test hello world root@bae226a5b14a:/# diff 1.test 2.test 1c1 < hello word --- > hello world root@bae226a5b14a:/# tail -f 2.test hello world ^C root@bae226a5b14a:/#
2.6. 查找文件内容
使用egrep查询文件内容:
egrep '03.1/CO/AE' TSF_STAT_111130.log.012
egrep 'A_LMCA777:C' TSF_STAT_111130.log.035 > co.out2
2.7. 文件与目录权限修改
- 改变文件的拥有者 chown
- 改变文件读、写、执行等属性 chmod
- 递归子目录修改: chown -R tuxapp source/
- 增加脚本可执行权限: chmod a+x myscript
2.8. 给文件增加别名
创建符号链接/硬链接:
ln cc ccAgain :硬连接;删除一个,将仍能找到;
ln -s cc ccTo :符号链接(软链接);删除源,另一个无法使用;(后面一个ccTo 为新建的文件)
2.9. 管道和重定向
- 批处理命令连接执行,使用 |
- 串联: 使用分号 ;
- 前面成功,则执行后面一条,否则,不执行:&&
- 前面失败,则后一条执行: ||
ls /proc && echo suss! || echo failed.
能够提示命名是否执行成功or失败;
与上述相同效果的是:
if ls /proc; then echo suss; else echo fail; fi
重定向:
ls proc/*.c > list 2> &l 将标准输出和标准错误重定向到同一文件;
等价的是:
ls proc/*.c &> list
清空文件:
:> a.txt
重定向:
echo aa >> a.txt
root@bae226a5b14a:/# echo hello word>.test root@bae226a5b14a:/# echo hello world>2.test root@bae226a5b14a:/# tail -5 2.test hello world root@bae226a5b14a:/# diff 1.test 2.test 1c1 < hello word --- > hello world root@bae226a5b14a:/# tail -f 2.test hello world ^C root@bae226a5b14a:/# ls /proc && echo suss! || echo failed 1 config.gz driver ioports kpagecgroup modules sched_debug sysrq-trigger vmallocinfo 14764 consoles execdomains irq kpagecount mounts self sysvipc vmstat acpi cpuinfo fb kallsyms kpageflags mpt slabinfo thread-self zoneinfo buddyinfo crypto filesystems kcore loadavg mtrr softirqs timer_list bus devices fs key-users locks net stat tty cgroups diskstats interrupts keys meminfo pagetypeinfo swaps uptime cmdline dma iomem kmsg misc partitions sys version suss! root@bae226a5b14a:/# ls /pan123 && echo suss! || echo failed ls: cannot access '/pan123': No such file or directory failed root@bae226a5b14a:/# echo aa >> a.txt root@bae226a5b14a:/# ls 1.test a.txt boot etc lib media opt root sbin sys usr 2.test bin dev home lib64 mnt proc run srv tmp var root@bae226a5b14a:/# cat a.txt aa root@bae226a5b14a:/# :> a.txt root@bae226a5b14a:/# cat a.txt root@bae226a5b14a:/# ls proc/*.c > list 2> &l bash: syntax error near unexpected token `&' root@bae226a5b14a:/# ls proc/*.c &> list root@bae226a5b14a:/# ls 1.test a.txt boot etc lib list mnt proc run srv tmp var 2.test bin dev home lib64 media opt root sbin sys usr root@bae226a5b14a:/# cat list ls: cannot access 'proc/*.c': No such file or directory root@bae226a5b14a:/# ls proc/*.c ls: cannot access 'proc/*.c': No such file or directory root@bae226a5b14a:/# ls proc/*.c >list2> &l bash: syntax error near unexpected token `&' root@bae226a5b14a:/# ls 1.test a.txt boot etc lib list mnt proc run srv tmp var 2.test bin dev home lib64 media opt root sbin sys usr root@bae226a5b14a:/# ls proc/*.c >list2&>l root@bae226a5b14a:/# ls 1.test a.txt boot etc l lib64 list2 mnt proc run srv tmp var 2.test bin dev home lib list media opt root sbin sys usr root@bae226a5b14a:/# cat 1 cat: 1: No such file or directory root@bae226a5b14a:/# cat list2 root@bae226a5b14a:/#
2.10. 设置环境变量
启动帐号后自动执行的是 文件为 .profile,然后通过这个文件可设置自己的环境变量;
安装的软件路径一般需要加入到path中:
PATH=$APPDIR:/opt/app/soft/bin:$PATH:/usr/local/bin:$TUXDIR/bin:$ORACLE_HOME/bin;export PATH
2.11. Bash快捷输入或删除
快捷键:
Ctl-U 删除光标到行首的所有字符,在某些设置下,删除全行
Ctl-W 删除当前光标到前边的最近一个空格之间的字符
Ctl-H backspace,删除光标前边的字符
Ctl-R 匹配最相近的一个文件,然后输出
root@bae226a5b14a:/# $Path root@bae226a5b14a:/# $path root@bae226a5b14a:/# $PATH bash: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin: No such file or directory root@bae226a5b14a:/# cat -v record.log | grep AAA | grep -v BBB | wc -l cat: record.log: No such file or directory 0 root@bae226a5b14a:/# cat AAABBBCCC>record.log cat: AAABBBCCC: No such file or directory root@bae226a5b14a:/# echo AAABBBCCC>record.log root@bae226a5b14a:/# cat -v record.log | grep AAA | grep -v BBB | wc -l 0 root@bae226a5b14a:/# echo AAACCC>record.log root@bae226a5b14a:/# cat -v record.log | grep AAA | grep -v BBB | wc -l 1 root@bae226a5b14a:/# echo BBBCCC>record.log root@bae226a5b14a:/# cat -v record.log | grep AAA | grep -v BBB | wc -l 0 root@bae226a5b14a:/#
2.13. 总结
文件管理,目录的创建、删除、查询、管理: mkdir rm mv
文件的查询和检索: find locate
查看文件内容:cat vi tail more
管道和重定向: ; | && >
原文地址:
https://linuxtools-rst.readthedocs.io/zh_CN/latest/base/02_file_manage.html
# dockerfile # base image FROM ubuntu:latest LABEL maintainer="Pan" # WORKDIR /app EXPOSE 3000 EXPOSE 35729 COPY sources.list /etc/apt RUN apt-get update # && apt-get install -y locales # && apt-get install debconf # && echo "Asia/Shanghai" > /etc/timezone # && dpkg-reconfigure -f noninteractive tzdata # && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime # && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen # && echo 'LANG="en_US.UTF-8"'>/etc/default/locale # && dpkg-reconfigure --frontend=noninteractive locales # && update-locale LANG=en_US.UTF-8 && apt-get install -y man && apt-get install manpages && apt-get install manpages-dev && apt-get install manpages-posix && apt-get install manpages-posix-dev && apt-get install -y vim && apt-get install -y info && apt-get install locate # CMD ["/bin"]