Linux文件系統–基礎學習–文件管理
Linux下的文件類型
–:普通文件 :這些文件一般是用一些相關的應用程序創建。它的第一個字符是 –
d: 目錄文件 :目錄在Linux是一個比較特殊的文件。它的第一個字符是 d
b: 塊設備 :這個種類的文件,是用mknode來創建,用rm來刪除,它的第一個字符是b
c: 字符設備 : 這表示字符設備文件。比如貓等串口設備,它的第一個字符是 c
l: 符號鏈接文件 : lrwxrwxrwx,注意第一個字符是l,這類文件是鏈接文件
p: 管道文件pipe : 簡單理解為數據通過管道輸出給別的程序使用(現在)
s: 套接字文件socket :當我們啟動MySQL服務器時,會產生一個mysql.sock的文件這個 文件的屬性的第一個字符是 s
[root@wCentos7 filesystemfenlei]# ll total 4 drwxr-xr-x. 2 root root 6 Jul 28 08:33 dir11 -rw-r--r--. 1 root root 73 Jul 28 08:33 file1 lrwxrwxrwx. 1 root root 4 Jul 28 08:36 rtc -> rtc0 crw-------. 1 root root 253, 0 Jul 28 08:36 rtc0 brw-rw----. 1 root disk 8, 0 Jul 28 08:36 sda
顯示當前工作目錄:pwd 變量$PWD $OLDPWD
[root@wCentos7 sbin]# pwd /usr/sbin [root@wCentos7 sbin]# echo $PWD /usr/sbin [root@wCentos7 sbin]# echo $OLDPWD /usr
pwd的路徑存儲在內部變量$PWD內;
oldpwd的上一次路徑變變量存儲在內部變量$OLDPWD中
[root@wCentos7 ~]# cd $OLDPWD 切換到上級目錄 [root@wCentos7 usr]# ll
絕對和相對路徑名
v絕對路徑:核心就是從 / 開始
以正斜杠開始
完整的文件的位置路徑
可用于任何想指定一個文件名的時候
舉例:
[root@wCentos7 usr]# cd /etc/sysconfig/network-scripts/
(只要是從根目錄開始的 / 我們就認為是絕對路徑)
v相對路徑名:不是從 / 開始
不以斜線開始
指定相對當前的工作目錄位置
可以作為一個簡短的形式指定一個文件名
[root@wCentos7 network-scripts]# cat ifcfg-eno16777736 ->使用相對路徑查看文件 TYPE=Ethernet BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=eno16777736
[root@wCentos7 network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736 TYPE=Ethernet 使用絕對路徑查看文件 BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=eno16777736
相對路徑和絕對路徑的對比
相對路徑和絕對路徑使用起來誰更方便呢?
這個沒有定論,要看具體情況,不同情況下誰更方便我們就是用哪一個。
例如:
[root@wCentos7 network-scripts]# cat ifcfg-eno16777736 ->使用相對路徑查看文件 [root@wCentos7 network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736
使用絕對路徑查看文件
以上肯定使用相對路徑要方便些,如果我們需要切換到根目錄 / 下,那肯定使用絕對路徑要方便點。
更改目錄命令: cd (Change directory )
功能:改變現在目錄到別的目錄
語法: cd [-L| [-P] [dir]
參數:
-P,如果目錄是符號鏈接,則進入實際的目錄;
-L,如果目錄是符號鏈接,則進行鏈接目錄;
舉例:
1.切換到根目錄:cd /
[root@wCentos7 ~]# cd / ==> 使用絕對路徑切換目錄
[root@wCentos7 /]# pwd
/
2.切換目錄到 testdir/shell目錄下:cd testdir/shell ==>使用相對路徑切換的,由于testdir本身在根目錄下
[root@wCentos7 /]# cd testdir/shell/ [root@wCentos7 shell]# pwd /testdir/shell
3.Linux 中有些特殊符號表示特定的含義:
.. : 表示上級目錄
. : 表示當前目錄
– : 表示上一次工作目錄
~ : 表示當前家目錄
~username : 表示指定用戶的家目錄
[root@wCentos7 shell]# pwd /testdir/shell [root@wCentos7 shell]# cd .. ==> 切換到上級目錄 [root@wCentos7 testdir]# pwd /testdir [root@wCentos7 testdir]# cd ./shell/ ==> 切換到下級目錄方式 [root@wCentos7 shell]# pwd /testdir/shell [root@wCentos7 shell]# cd - ==> 切換到上此工作目錄 /testdir [root@wCentos7 testdir]# cd ~ ==> 切換到自己家目錄 [root@wCentos7 ~]# pwd /root [root@wCentos7 ~]# cd ~chen ==> 切換到陳的家目錄 [root@wCentos7 ChenJiaShun]# pwd /home/ChenJiaShun
列出目錄內容命令 : ls
功能說明:最基本的就是顯示系統上有哪些文件
語法:ls [OPTION]… [FILE]…
參數:
ls -a包含隱藏文件(隱藏文件以.號開始)
ls -l顯示額外的信息(顯示文件權限,大小,屬組,屬主等信息)
ls -R目錄遞歸通過(顯示目錄下面的文件)
ls -ld目錄和符號鏈接信息
ls -1 文件分行顯示
ls –S 按從大到小排序
ls –u 配合-t選項,顯示并按atime從新到舊排序
ls –U 不排序按目錄存放順序顯示
舉例:
[root@wCentos7 ~]# ls -->正常ls 顯示不會顯示隱藏文件和文件其它屬性 anaconda-ks.cfg Downloads initial-setup-ks.cfg Public zzzzzzz Desktop f11 Music Templates Documents f1.txt Pictures Videos [root@wCentos7 ~]# ls -a -->顯示該目錄下是所有的文件和文件夾(. ..是當前目錄和上級目錄的 ) . .config f1.txt Templates .. .cshrc .ICEauthority Videos anaconda-ks.cfg .dbus initial-setup-ks.cfg .viminfo .bash_history Desktop .local .Xauthority .bash_logout Documents Music zzzzzzz .bash_profile Downloads Pictures .bashrc .esd_auth Public .cache f11 .tcshrc
[root@wCentos7 ~]# ls -R -->顯示該目錄下是所有的文件和文件夾下面的文件和目錄.: anaconda-ks.cfg Downloads initial-setup-ks.cfg Public zzzzzzz Desktop f11 Music Templates Documents f1.txt Pictures Videos ./Desktop: ./Documents: test ./Documents/test: 123 text2 ./Documents/test/text2: 456 test3 ./Documents/test/text2/test3: 789 ./Downloads: fuse.conf ./Music: ./Pictures: ./Public: ./Templates: ./Videos:
[root@wCentos7 ~]# ls -ld -->顯示當前目錄的文件詳細信息 dr-xr-x---. 14 root root 4096 Jul 28 08:36 .
[root@wCentos7 ~]# ls -Sl -->當前目錄下文件按文件大小排序(注意沒有統計目錄下文件大小的合計) total 16 -rw-------. 1 root root 1465 Jul 21 11:35 initial-setup-ks.cfg -rw-------. 1 root root 1417 Jul 21 11:34 anaconda-ks.cfg drwxr-xr-x. 2 root root 22 Jul 28 11:39 Downloads drwxr-xr-x. 3 root root 17 Jul 27 23:14 Documents -rwxrw-rw-. 1 root root 15 Jul 27 10:15 f1.txt -rw-r--r--. 1 root root 12 Jul 27 10:19 f11 drwxr-xr-x. 2 root root 6 Jul 21 11:54 Desktop drwxr-xr-x. 2 root root 6 Jul 21 11:54 Music drwxr-xr-x. 2 root root 6 Jul 21 11:54 Pictures drwxr-xr-x. 2 root root 6 Jul 21 11:54 Public drwxr-xr-x. 2 root root 6 Jul 21 11:54 Templates drwxr-xr-x. 2 root root 6 Jul 21 11:54 Videos -rw-r--r--. 1 root root 0 Jul 27 23:24 zzzzzzz
[root@wCentos7 ~]# ls -utl-->當前目錄下文件按訪問時間前后順序排序的 total 16 drwxr-xr-x. 2 root root 22 Jul 28 11:39 Downloads -rw-r--r--. 1 root root 0 Jul 27 23:24 zzzzzzz drwxr-xr-x. 3 root root 17 Jul 27 23:14 Documents drwxr-xr-x. 2 root root 6 Jul 27 16:38 Music drwxr-xr-x. 2 root root 6 Jul 27 16:38 Pictures drwxr-xr-x. 2 root root 6 Jul 27 16:38 Videos drwxr-xr-x. 2 root root 6 Jul 27 16:38 Desktop -rw-------. 1 root root 1465 Jul 27 16:38 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 Jul 27 16:38 Public drwxr-xr-x. 2 root root 6 Jul 27 16:38 Templates -rw-------. 1 root root 1417 Jul 27 16:38 anaconda-ks.cfg -rw-r--r--. 1 root root 12 Jul 27 10:19 f11 -rwxrw-rw-. 1 root root 15 Jul 27 10:15 f1.txt
關于文件名通配符的描述
匹配模式:元字符
* : 匹配任意長度任意字符;
例如: pa* = paaa,pab,pa,pac,pawwsd。
? :匹配任意單個字符(不包含零字符);
例如:pa? =pas,pa1,pab,pa2(不包含pa)。
[] :范圍內的任意單個字符;
格式: [a-z]所有的字母,不區分大小寫; [0-9]所有的數字
例如: pa[0-9][0-9] = pa00~~pa99
2[0-9][0-9] = 200~~299
特殊的文件格式:
[[:upper:]] :大寫 的A-Z
[[:lower:]] :小寫的 a-z
[[:alpha:]] :所有的字母
[[:digit:]] :所有的數字
[[:alnum:]] :所有的字母與數字
[[:space:]] :空白字符
[[:punct:]] :所有的標點符號
[^] :匹配指定范圍外的任意字符
練習題 熟練對命令和通配符的理解
1、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位數字的文件或目錄
ll -ad /var/l*[[:digit:]] *[[:lower:]] ==> l開頭+任意字符+所有數字+任意字符+小寫字母
ll -ad /var/l*[[:digit:]*[a-z] ==> l開頭+任意字符+所有數字+任意字符+a-z
ll -ad /var/l*[0-9]*[a-z] ==> l開頭+任意字符+0-9+任意字符+a-z(只有這個可以達到我需要的效果)
注意:理論上三個通配符都可以達到我的要求,我就奇怪為什么只有最下面的一個可以,難道是我電腦問題。
[root@wCentos7 var]# ll -d /var/l*[0-9]*[a-z]
-rw-r–r–. 1 root root 0 Jul 28 12:28 /var/l123name
2、顯示/etc目錄下以任意一位數字開頭,且以非數字結尾的文件或目錄
ll –ad /etc/[0-9]*[^0-9] ==> 0-9開頭+任意字符+非0-9結尾
[root@wCentos7 var]# touch /etc/{222ww,333txt,444uu5} [root@wCentos7 var]# ll -d /etc/[0-9]*[^0-9] -rw-r--r--. 1 root root 0 Jul 28 12:47 /etc/222ww -rw-r--r--. 1 root root 0 Jul 28 12:47 /etc/333txt [root@wCentos7 var]#
3、顯示/etc/目錄下以非字母開頭,后面跟了一個字母及其它任意長度任意字符的文件或目錄
ll -ad /etc/[^[:alpha:]][[:alpha:]]* ==>非字母開頭+任意一個字母
[root@wCentos7 var]# ll -ad /etc/[^[:alpha:]][[:alpha:]]* -rw-r--r--. 1 root root 0 Jul 28 12:52 /etc/1wwq
4、顯示/etc目錄下所有以m開頭以非數字結尾的文件或目錄
ll -ad /etc/m*[^[::digit]] ==>m開頭+任意字符+非數字結尾
[root@wCentos7 var]# ll -ad /etc/m*[^[:digit:]] -rw-r--r--. 1 root root 0 Jul 28 12:57 /etc/m12b -rw-r--r--. 1 root root 0 Jul 28 12:57 /etc/m1qw -r--r--r--. 1 root root 33 Jul 21 11:27 /etc/machine-id -rw-r--r--. 1 root root 111 Nov 20 2015 /etc/magic -rw-r--r--. 1 root root 1968 Dec 17 2014 /etc/mail.rc -rw-r--r--. 1 root root 5122 Nov 21 2015 /etc/makedumpfile.conf.sample -rw-r--r--. 1 root root 5171 Jun 10 2014 /etc/man_db.conf drwxr-xr-x. 2 root root 6 Nov 21 2015 /etc/maven -rw-r--r--. 1 root root 936 Mar 6 2015 /etc/mke2fs.conf drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/modprobe.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/modules-load.d -rw-r--r--. 1 root root 0 Jun 7 2013 /etc/motd lrwxrwxrwx. 1 root root 17 Jul 21 11:22 /etc/mtab -> /proc/self/mounts -rw-r--r--. 1 root root 2620 Jun 10 2014 /etc/mtools.conf drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/multipath -rw-r--r--. 1 root root 570 Nov 21 2015 /etc/my.cnf drwxr-xr-x. 2 root root 30 Jul 21 11:25 /etc/my.cnf.d
5、顯示/etc目錄下,所有以.d結尾的文件或目錄
ll -ad /etc/*.d ==>任意字符+.d結尾
[root@wCentos7 var]# ll -ad /etc/*.d drwxr-xr-x. 2 root root 4096 Jul 21 11:31 /etc/bash_completion.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/binfmt.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/chkconfig.d drwxr-xr-x. 2 root root 51 Jul 21 11:30 /etc/cron.d drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/depmod.d drwxr-xr-x. 2 root root 6 Aug 6 2015 /etc/dnsmasq.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/dracut.conf.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/exports.d drwxr-xr-x. 2 root root 6 Sep 11 2015 /etc/gdbinit.d drwx------. 2 root root 4096 Jul 21 11:34 /etc/grub.d lrwxrwxrwx. 1 root root 11 Jul 21 11:23 /etc/init.d -> rc.d/init.d drwx------. 5 root root 4096 Jul 21 11:27 /etc/ipsec.d drwxr-xr-x. 2 root root 4096 Jul 21 11:27 /etc/ld.so.conf.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/libibverbs.d drwxr-xr-x. 2 root root 4096 Jul 21 11:30 /etc/logrotate.d drwxr-xr-x. 2 root root 22 Jul 21 11:27 /etc/modprobe.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/modules-load.d drwxr-xr-x. 2 root root 30 Jul 21 11:25 /etc/my.cnf.d drwxr-xr-x. 2 root root 68 Jul 21 11:27 /etc/oddjobd.conf.d drwxr-xr-x. 2 root root 4096 Jul 21 17:11 /etc/pam.d drwxr-xr-x. 2 root root 6 Jun 10 2014 /etc/popt.d drwxr-xr-x. 2 root root 101 Jul 21 11:27 /etc/prelink.conf.d drwxr-xr-x. 2 root root 4096 Jul 21 11:30 /etc/profile.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc0.d -> rc.d/rc0.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc1.d -> rc.d/rc1.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc2.d -> rc.d/rc2.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc3.d -> rc.d/rc3.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc4.d -> rc.d/rc4.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc5.d -> rc.d/rc5.d lrwxrwxrwx. 1 root root 10 Jul 21 11:23 /etc/rc6.d -> rc.d/rc6.d drwxr-xr-x. 10 root root 4096 Nov 20 2015 /etc/rc.d drwxr-xr-x. 2 root root 29 Jul 21 11:27 /etc/request-key.d drwxr-xr-x. 2 root root 51 Jul 21 11:27 /etc/rsyslog.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/rwtab.d drwxr-xr-x. 3 root root 4096 Jul 21 11:29 /etc/sane.d drwxr-xr-x. 2 root root 4096 Jul 21 11:30 /etc/setuptool.d drwxr-xr-x. 2 root root 6 Nov 20 2015 /etc/statetab.d drwxr-x---. 2 root root 6 Nov 21 2015 /etc/sudoers.d drwxr-xr-x. 2 root root 27 Jul 21 11:27 /etc/sysctl.d drwxr-xr-x. 2 root root 24 Nov 20 2015 /etc/tmpfiles.d drwxr-xr-x. 2 root root 8192 Jul 21 11:27 /etc/usb_modeswitch.d drwxr-xr-x. 2 root root 6 Aug 12 2015 /etc/xinetd.d drwxr-xr-x. 2 root root 4096 Dec 3 2015 /etc/yum.repos.d
6、顯示/etc目錄下,所有.conf結尾,且以m,n,r,p開頭的文件或目錄
ll -ad /etc/[mnrp]*.conf ==>mnrp任意一個字符開頭+任意字符+.conf結尾
[root@wCentos7 var]# ll -ad /etc/[mnrp]*.conf -rw-r--r--. 1 root root 5171 Jun 10 2014 /etc/man_db.conf -rw-r--r--. 1 root root 936 Mar 6 2015 /etc/mke2fs.conf -rw-r--r--. 1 root root 2620 Jun 10 2014 /etc/mtools.conf -rw-r--r--. 1 root root 3390 Nov 20 2015 /etc/nfsmount.conf -rw-r--r--. 1 root root 1717 Jul 21 11:34 /etc/nsswitch.conf -rw-r--r--. 1 root root 91 Dec 3 2012 /etc/numad.conf -rw-r--r--. 1 root root 1362 Jun 10 2014 /etc/pbm2ppa.conf -rw-r--r--. 1 root root 6300 Jun 10 2014 /etc/pnm2ppa.conf -rw-r--r--. 1 root root 433 Sep 9 2015 /etc/radvd.conf -rw-r--r--. 1 root root 1787 Jun 10 2014 /etc/request-key.conf -rw-r--r--. 1 root root 63 Jul 28 08:36 /etc/resolv.conf -rw-r--r--. 1 root root 458 Nov 21 2015 /etc/rsyncd.conf -rw-r--r--. 1 root root 3232 Sep 8 2015 /etc/rsyslog.conf
查看文件狀態
詳細請見 博客 關于文件 atime mtime gtime 專題
創建空文件和刷新時間:touch命令
vtouch命令:
功能:創建空文件和刷新時間
語法:touch [OPTION]… FILE…
參數:
-a: 僅改變atime
-m: 僅改變mtime
-t:STAMP:
[[CC]YY]MMDDhhmm[.ss]
-c: 如果文件不存在,則不予創建
舉例:
[root@wCentos7 touch.dir]# ll -a -->當前目錄下沒有任何文件 total 4 drwxr-xr-x. 2 root root 6 Jul 28 15:32 . drwxr-xr-x. 14 root root 4096 Jul 28 15:32 .. [root@wCentos7 touch.dir]# t ouch 11 -->touch一個不存在的文件 [root@wCentos7 touch.dir]# ll -a -->當前目錄下新建了一個文件 total 4 drwxr-xr-x. 2 root root 15 Jul 28 15:32 . drwxr-xr-x. 14 root root 4096 Jul 28 15:32 .. -rw-r--r--. 1 root root 0 Jul 28 15:32 11 [root@wCentos7 touch.dir]# stat 11 #查看當前文件的狀態為當前時間 File: ‘11’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 3142 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-28 15:32:38.911943667 +0800 Modify: 2016-07-28 15:32:38.911943667 +0800 Change: 2016-07-28 15:32:38.911943667 +0800 Birth: - [root@wCentos7 touch.dir]# touch -a -m 11 #修改 atime mtime的時間 [root@wCentos7 touch.dir]# stat 11 File: ‘11’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 3142 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2016-07-28 15:33:33.499942685 +0800 Modify: 2016-07-28 15:33:33.499942685 +0800 # 文件的三個時間都有變化 Change: 2016-07-28 15:33:33.499942685 +0800 Birth: - [root@wCentos7 touch.dir]# touch -t 200808080808.08 11 #修改時間的為2008年 [root@wCentos7 touch.dir]# stat 11 File: ‘11’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 3142 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:etc_runtime_t:s0 Access: 2008-08-08 08:08:08.000000000 +0800 Modify: 2008-08-08 08:08:08.000000000 +0800 #atime mtime的時間都變為修改的時間 Change: 2016-07-28 15:34:12.920941975 +0800 Birth: - [root@wCentos7 touch.dir]# touch -c 22 #文件不存在,就不會創建文件,文件存在就會修改文件時間 [root@wCentos7 touch.dir]# ll -a total 4 drwxr-xr-x. 2 root root 15 Jul 28 15:32 . drwxr-xr-x. 14 root root 4096 Jul 28 15:32 .. -rw-r--r--. 1 root root 0 Jul 28 15:40 11 [root@wCentos7 touch.dir]#
復制文件和目錄cp 命令
功能:cp copy復制文件和目錄,并且可以改名
語法:
cp [OPTION]… [-T] SOURCE DEST
vcp [OPTION]… SOURCE… DIRECTORY (第二個語法常用)
vcp [OPTION]… -t DIRECTORY SOURCE…
注意:舉例說明可以比較好說明
如果目標不存在:新建目標文件,并將源文件中內容填充至目標中
如果目標存在:
如果目標是文件:將源文件中的內容覆蓋至目標文件中
基于安全,建議為cp命令使用-i選項
如果目標是目錄:在目標下新建與原文件同名的文件,并將源文件中內容填充至新文件中
參數:
-i:交互式 (目標文件有同名,提示需要覆蓋嗎?)
-r, -R: 遞歸復制目錄及內部的所有內容;(主要用于目錄拷貝)
-d:–no-dereference –preserv=links 不復制原文件,只復制鏈接名
–preserv[=ATTR_LIST] (復制的時候對于文件的屬性是否變更問題)
mode: 權限
ownership: 屬主屬組
timestamp:
links
xattr
context
all
-a: 歸檔,相當于-dR–preserv=all
-p: 等同–preserv=mode,ownership,timestamp(拷貝后文件屬主 屬組 時間戳不改變)
-v: –verbose (顯示其拷貝過程)
-f: –force(強制)
舉例:
1.拷貝單個文件,目標不存在(會在目標目錄下新建一個同名文件)
[root@wCentos7 touch.dir]# cp /etc/issue ./ #拷貝文件到當前目錄下 [root@wCentos7 touch.dir]# ll total 4 -rw-r--r--. 1 root root 0 Jul 28 15:40 11 -rw-r--r--. 1 root root 126 Jul 28 15:58 issue
2.如果目標文件存在,提示會覆蓋目標文件,沒有使用-i參數為什么提示我覆蓋文件呢?
[root@wCentos7 touch.dir]# cp /etc/issue issue issue.net [root@wCentos7 touch.dir]# cp /etc/issue ./ cp: overwrite ‘./issue’? Y
[root@wCentos7 touch.dir]# alias #由于是管理員登錄,默認cp命令的別名有-i參數,所以提示覆蓋,安全設 alias cls='clear' 置而已,如果是普通用戶登錄,目標存在,是不會提醒覆蓋,就是沒有-i選項 alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias ip='ifconfig' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias vimnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
3.如果源文件為目錄,目標不存在,會在目標目錄下新建一個同名的目錄
[root@wCentos7 touch.dir]# cp /etc/sysconfig/network-scripts/ ./ #拷貝目錄一定記得要帶-r 參數,不讓會報錯 cp: omitting directory ‘/etc/sysconfig/network-scripts/’ [root@wCentos7 touch.dir]# cp -r /etc/sysconfig/network-scripts/ ./ #加上-r參數就不報錯了 [root@wCentos7 touch.dir]# ll total 8 -rw-r--r--. 1 root root 0 Jul 28 15:40 11 -rw-r--r--. 1 root root 126 Jul 28 15:59 issue drwxr-xr-x. 2 root root 4096 Jul 28 16:08 network-scripts #創建了一個同名的目錄 [root@wCentos7 touch.dir]#
4.如果目標目錄存在,則源目錄會拷貝至目標目錄的同名目錄文件里面去
[root@wCentos7 touch.dir]# cp -r /etc/sysconfig/network-scripts/ ./network-scripts/[root@wCentos7 touch.dir]# tree -d -L 2.└── network-scripts └── network-scripts
其實我們的初衷是想讓network-scripts/下的文件夾覆蓋掉我們目標目錄/network-scripts/下的所有文件,下面我就介紹下如何讓源和目標同名的目錄,源目錄下的文件覆蓋目標目錄下的文件。
5.如果目標目錄存在,且源目錄和目標目錄同名,我們如何讓源目錄下的文件覆蓋目標同名目錄下的文件
[root@wCentos7 touch.dir]# cp -r /etc/sysconfig/network-scripts/ ./ cp: overwrite ‘./network-scripts/ifdown-Team’? y cp: overwrite ‘./network-scripts/ifdown-TeamPort’? y cp: overwrite ‘./network-scripts/ifup-Team’? y cp: overwrite ‘./network-scripts/ifup-TeamPort’? y cp: overwrite ‘./network-scripts/ifcfg-eno16777736’? y [root@wCentos7 touch.dir]# \cp -rf /etc/sysconfig/network-scripts/ ./ [root@wCentos7 touch.dir]#
注意: cp -r /etc/sysconfig/network-scripts/ ./
這個命令是可以更新目標目錄下的文件,不過由于默認情況下管理員賬號是有 -i的選項,需要提示你是不是確定覆蓋文件,如果有100個是不是很煩的,那我們就可以使用下面的方法。
\cp -rf /etc/sysconfig/network-scripts/ ./
讓cp使用原本沒有-i選項 帶上-f參數即可強制覆蓋目錄文件目錄下的文件
-p參數的使用,不改變文件的屬主 屬組和時間戳
[root@wCentos7 ~]# cp ~chen/bash.sh ./ #默認復制后 文件的屬組和屬組都變為root用戶的 [root@wCentos7 ~]# stat bash.sh File: ‘bash.sh’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 207797038 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2016-07-28 16:33:58.340877444 +0800 Modify: 2016-07-28 16:33:58.340877444 +0800 Change: 2016-07-28 16:33:58.340877444 +0800 Birth: -
[root@wCentos7 ~]# cp -p ~chen/bash.sh ./bash1.sh #-p參數拷貝一份后 [root@wCentos7 ~]# stat bash1.sh #保留原來的屬主和屬組 File: ‘bash1.sh’ Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 802h/2050d Inode: 207797039 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ chen) Gid: ( 1000/ChenJiaShun) Context: unconfined_u:object_r:admin_home_t:s0 Access: 2016-07-28 16:33:58.340877444 +0800 Modify: 2016-07-28 16:33:11.242878292 +0800 Change: 2016-07-28 16:34:24.988876964 +0800 Birth: -
-d參數的使用,不復制源文件,只復制鏈接文件,默認情況下復制連接的源文件,
[root@wCentos7 etc]# cp system-release /testdir/
# 正常拷貝連接文件 system-release到目錄目錄下
[root@wCentos7 etc]# cp -d system-release /testdir/system-release2
# -p拷貝連接文件 system-release到目錄目錄下
[root@wCentos7 etc]# cd /testdir/
[root@wCentos7 testdir]# ll
-rw-r–r–. 1 root root 38 Jul 28 16:38 system-release
# 正??截愡B接文件 system-release到目錄目錄下,為正常的普通文件
lrwxrwxrwx. 1 root root 14 Jul 28 16:39 system-release2 -> centos-release
# -p拷貝連接文件 system-release到目錄目錄下,為連接文件,由于源文件找不到,所以會一致是紅色的閃閃的
[root@wCentos7 testdir]#
命令別名: alias命令詳解
alias命令
功能:可以給一個較長的命令設置一個讓我們好記憶的名稱
語法:
alisa NAME=”VALUE” 設置別名
unalias NAME 取消別名
注意:在命令行中定義的別名,僅對當前shell進程有效
v 如果想永久有效,要定義在配置文件中
僅對當前用戶:~/.bashrc
對所有用戶有效:/etc/bashrc
bash進程重新讀取配置文件:
source /path/to/config_file
. /path/to/config_file
參數:
-p : 打印出系統現有設置的別名
舉例:
1.查看系統現有別名
[root@wCentos7 testdir]# alias -p alias cp='cp -i' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto' alias grep='grep --color=auto' alias ip='ifconfig' alias l.='ls -d .* --color=auto' alias ll='ls -l --color=auto' alias ls='ls --color=auto' alias mv='mv -i' alias rm='rm -i' alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
2.設置別名(最好不要設置別名名稱和現有命令名稱一樣的別名)
alias cls='clear' #cls 代替命令 clear 清屏命令 , 原來的clear命令還是可用 alias vimnet='vim /etc/sysconfig/network-scripts/ifcfg-eno16777736'
#設置 vimnet 別名代替配置網卡信息的命令+路徑
注意:只對當前shell 進程用戶有效,需要永久有效就修改配置文件
3.修改配置文件讓別名永久有效(僅對當前用戶:~/.bashrc 對所有用戶有效:/etc/bashrc)
[root@wCentos7 testdir]# cat ~/.bashrc # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' alias cls="clear" alias ip="ifconfig" #定義alias別名 alias vimnet="vim /etc/sysconfig/network-scripts/ifcfg-eno16777736" # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi [root@wCentos7 testdir]# [root@wCentos7 testdir]# source /root/.bashrc #使配置文件立即生效
移動和重命名文件
移動和重命名文件:mv
功能:mv命令:移動文件或目錄 還可以改名
語法:mv [OPTION]… SOURCE… DIRECTORY
參數:
-i: 交互式 (目標文件存在提示覆蓋,需要確認)
-f: 強制刪除 (不確認)
舉例:
[root@wCentos7 mydate]# mv system-release2 system-release #移動覆蓋提示確認 mv: overwrite ‘system-release’? Y [root@wCentos7 mydate]# mv -f 111 system-release #移動覆蓋不提示 [root@wCentos7 mydate]# ll total 0 -rw-r--r--. 1 root root 0 Jul 28 17:51 system-release dr-xr-xr-x. 2 root root 93 Jul 26 13:25 text-dir
刪除文件或目錄 rm
功能:刪除文件或目錄
語法:rm [OPTION]… FILE…
參數:
-i: 交互式
-f: 強制刪除
-r: 遞歸
舉例:
1.刪除文件:(默認別名有 -i選項)[root@wCentos7 mydate]# rm system-release rm: remove regular empty file ‘system-release’? y
2.刪除目錄:(刪除目錄需要添加 -r選項)
[root@wCentos7 mydate]# rm bashtest/ rm: cannot remove ‘bashtest/’: Is a directory [root@wCentos7 mydate]# rm -r bashtest/ rm: descend into directory ‘bashtest/’? y rm: remove regular file ‘bashtest/1.sh’? y rm: remove regular file ‘bashtest/.1.sh.swp’? y rm: remove regular empty file ‘bashtest/222’? y rm: remove regular empty file ‘bashtest/333’? y rm: remove regular empty file ‘bashtest/444’? y rm: remove regular empty file ‘bashtest/555’? y rm: remove regular empty file ‘bashtest/666’? y rm: remove regular empty file ‘bashtest/777’? y rm: remove directory ‘bashtest/’? y
3.強制刪除目錄:(刪除文件和目錄不確認通用使用rm -rf 命令 )
[root@wCentos7 mydate]# touch system.txt [root@wCentos7 mydate]# rm -rf system.txt [root@wCentos7 mydate]#
v
目錄操作命令:tree rmdir mkdir
vtree 命令
功能:顯示目錄樹,顯示目錄結構
語法: tree [option] [directory …]
參數:
-d: 只顯示目錄
-L level:指定顯示的層級數目
-P pattern: 只顯示由指定pattern匹配到的路徑
舉例:
1.顯示家目錄結構
[root@wCentos7 ~]# tree . ├── anaconda-ks.cfg ├── bash1.sh ├── bash.sh ├── Desktop ├── Documents │ └── test │ ├── 123 │ └── text2 │ ├── 456 │ └── test3 │ └── 789 ├── Downloads │ └── fuse.conf ├── f11 ├── f1.txt ├── initial-setup-ks.cfg ├── Music ├── Pictures ├── Public ├── Templates ├── Videos └── zzzzzzz
2.只顯示目錄,不顯示文件
[root@wCentos7 ~]# tree -d . ├── Desktop ├── Documents │ └── test │ └── text2 │ └── test3 ├── Downloads ├── Music ├── Pictures ├── Public ├── Templates └── Videos
3.只顯示當前目錄下的目錄結構,不顯示目錄下的目錄
[root@wCentos7 ~]# tree -d -L 1.├── Desktop├── Documents├── Downloads├── Music├── Pictures├── Public├── Templates└── Videos
vmkdir創建目錄
功能:創建命令
語法: mkdir [OPTION]… DIRECTORY…
參數:
-p: 存在于不報錯,且可自動創建所需的各目錄;
-v: 顯示詳細信息
-m MODE: 創建目錄時直接指定權限;(前期沒有講解,后期講解)
舉例:
1.批量創建空目錄并顯示其建立目錄的過程
[root@wCentos7 dir3]# mkdir dir1/dir2/dir3/dir4 #不加-p 參數是無法創建目錄中的目錄的 mkdir: cannot create directory ‘dir1/dir2/dir3/dir4’: No such file or directory [root@wCentos7 dir3]# mkdir-pv dir1/dir2/dir3/dir4 bash: mkdir-pv: command not found... [root@wCentos7 dir3]# mkdir -pv dir1/dir2/dir3/dir4 #加上-pv參數,批量創建目錄中的目錄并顯示器創建過程 mkdir: created directory ‘dir1’ mkdir: created directory ‘dir1/dir2’ mkdir: created directory ‘dir1/dir2/dir3’ mkdir: created directory ‘dir1/dir2/dir3/dir4’ [root@wCentos7 dir3]#
vrmdir刪除空目錄
功能:刪除空目錄
語法:mkdir [OPTION]… DIRECTORY…
參數:
-p: 遞歸刪除父空目錄
-v: 顯示詳細信息
vrm-r遞歸刪除目錄樹
舉例:只用于刪除空目錄,實際用得比較少常用 :rm-r遞歸刪除目錄樹
原創文章,作者:linux_root,如若轉載,請注明出處:http://www.www58058.com/27689