Linux:文件处理、目录处理、链接文件

1. 文件处理

创建文件: touch "文件名"

# touch test.txt

查看文件:ls -l "文件名"   或 ll “文件名”    或  file “文件名”

 # ll test.txt
-rw-r----- 1 root root 0 Sep 11 16:29 test.txt
You have new mail in /var/mail/root
~ # ls -l test.txt
-rw-r----- 1 root root 0 Sep 11 16:29 test.txt
~ # file test.txt
test.txt: empty
~ #

 编辑文件内容:vim "文件名"

复制文件: cp "源文件名" “目的文件名”  

   加 -i 参数时,会在覆盖前提示.

   -R参数,则是递归的复制整个目录+子目录+文件


shaphicprb13137:~ # cp test.txt test.bkp
shaphicprb13137:~ # ll test.*
-rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:32 test.txt
You have new mail in /var/mail/root
shaphicprb13137:~ # ll -i test.*
574645 -rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
576495 -rw-r----- 1 root root 21 Sep 11 16:32 test.txt

shaphicprb13137:~ # ll -R test
test:
total 12
-rw-r----- 1 root root 21 Sep 11 16:34 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:32 test.txt
shaphicprb13137:~ # ll -R test*
test:
total 12
-rw-r----- 1 root root 21 Sep 11 16:34 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:32 test.txt
shaphicprb13137:~ # cp -R test test2
shaphicprb13137:~ # ll -R test*
test:
total 12
-rw-r----- 1 root root 21 Sep 11 16:34 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:32 test.txt

test2:
total 12
-rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:38 test.txt

文件重命名:mv "源文件" “目标文件”

如下可以看到 文件的inode编号未改变,只是名字发生了变化


shaphicprb13137:~/test # ls -li test*
576491 -rw-r----- 1 root root 21 Sep 11 16:34 test2.txt
574645 -rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
576495 -rw-r----- 1 root root 21 Sep 11 16:32 test.txt
shaphicprb13137:~/test # mv test2.txt test3
shaphicprb13137:~/test # ls -li test*
576491 -rw-r----- 1 root root 21 Sep 11 16:34 test3
574645 -rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
576495 -rw-r----- 1 root root 21 Sep 11 16:32 test.txt

文件删除: rm "文件名"

添加-i参数时,会在删除前先询问.


shaphicprb13137:~/test # ls -li test*
576491 -rw-r----- 1 root root 21 Sep 11 16:34 test3
574645 -rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
576495 -rw-r----- 1 root root 21 Sep 11 16:32 test.txt
shaphicprb13137:~/test # rm -i test3
rm: remove regular file 'test3'? y
You have new mail in /var/mail/root
shaphicprb13137:~/test # ls -li test*
574645 -rw-r----- 1 root root 21 Sep 11 16:33 test.bkp
576495 -rw-r----- 1 root root 21 Sep 11 16:32 test.txt
shaphicprb13137:~/test #

查看文件内容:

1. cat "文件名": 查看文件的所有内容。缺点:只能查看全部内容,不能操作,不能选看。

添加-n参数,可以查看行号;

添加-b参数,只给有文本的行添加行号;


shaphicprb13137:~/test2 # cat test.txt
This is a test file.
This is a test file.
~

This is a test file.
~

This is a test file.
~

shaphicprb13137:~/test2 # cat -n test.txt
     1  This is a test file.
     2  This is a test file.
     3  ~
     4
     5  This is a test file.
     6  ~
     7
     8  This is a test file.
     9  ~
    10
You have new mail in /var/mail/root
shaphicprb13137:~/test2 # cat -b test.txt
     1  This is a test file.
     2  This is a test file.
     3  ~

     4  This is a test file.
     5  ~

     6  This is a test file.
     7  ~

2. more和less:

more:可以按页查看文件内容,可以使用“空格键”或“回车符”翻页,只能往下翻页;支持“/”后查找

less: 功能比more更为强大,不止可以往下翻页,可以识别上下键按行查看,也支持上下翻页键按页查看。支持回看。

3.tail和head: 查看部分内容

tail是查看文件的尾部,默认查看最后10行,可以使用 -n "行号" 查看最后n行的内容;

head是查看文件头部内容,默认查看最前面的10行,可以使用 -"行号" 查看前面n行的内容。

 


shaphicprb13137:~ # head /etc/bash.bashrc
# /etc/bash.bashrc for SUSE Linux
#
# PLEASE DO NOT CHANGE /etc/bash.bashrc There are chances that your changes
# will be lost during system upgrades.  Instead use /etc/bash.bashrc.local
# for bash or /etc/ksh.kshrc.local for ksh or /etc/zsh.zshrc.local for the
# zsh or /etc/ash.ashrc.local for the plain ash bourne shell  for your local
# settings, favourite global aliases, VISUAL and EDITOR variables, etc ...

#
# Check which shell is reading this file
shaphicprb13137:~ # head -5 /etc/bash.bashrc
# /etc/bash.bashrc for SUSE Linux
#
# PLEASE DO NOT CHANGE /etc/bash.bashrc There are chances that your changes
# will be lost during system upgrades.  Instead use /etc/bash.bashrc.local
# for bash or /etc/ksh.kshrc.local for ksh or /etc/zsh.zshrc.local for the
shaphicprb13137:~ # tail /etc/bash.bashrc
esac
test -s /etc/sh.shrc.local && . /etc/sh.shrc.local

if test -n "$restricted" -a -z "$PROFILEREAD" ; then
    PATH=/usr/lib/restricted/bin
    export PATH
fi
#
# End of /etc/bash.bashrc
#
shaphicprb13137:~ # tail -n 7 /etc/bash.bashrc
if test -n "$restricted" -a -z "$PROFILEREAD" ; then
    PATH=/usr/lib/restricted/bin
    export PATH
fi
#
# End of /etc/bash.bashrc
#
shaphicprb13137:~ #

 

2. 目录处理

创建文件:mkdir "目录名"

查看目录: ls -R "目录名"

shaphicprb13137:~ # ls -R test*
test:
test.bkp  test.txt

test2:
test2.txt  test.bkp  test.txt
shaphicprb13137:~ #

删除目录:rmdir 或rd 或rm -d

linux删除文件夹时只能删除空目录,如果目录下有文件,删除会失败。

shaphicprb13137:~ # ll -R test*
test:
total 12
-rw-r----- 1 root root 21 Sep 11 16:48 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:48 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:48 test.txt

test2:
total 12
-rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
-rw-r----- 1 root root 21 Sep 11 16:38 test.txt
shaphicprb13137:~ # rmdir test
rmdir: failed to remove 'test': Directory not empty
You have new mail in /var/mail/root
shaphicprb13137:~ # rd test
rmdir: failed to remove 'test': Directory not empty
shaphicprb13137:~ # rm -d test
rm: cannot remove 'test': Directory not empty
shaphicprb13137:~ #

这种情况下可以使用 -r参数,能够递归删除所有的子目录和子文件:

shaphicprb13137:~ # rm -r test
shaphicprb13137:~ #

 

3. 链接文件

链接文件是Linux特有的特性,类似于windows中的快捷方式。

如果需要在系统中维护同一文件的两份或者多分副本,可以采用保存文件的一个物理文件副本和多个虚拟副本的方法,这种虚拟的副本就是链接。链接是目录中指向文件真实位置的占位符

链接文件分为符号链接和硬链接。

  • 符号链接:是一个实实在在的文件,执行存放在虚拟目录结构中某个地方的另一个文件,这两个通过符号链接连接在一起的文件,内容并不相同。通过ln命令和-s参数创建链接文件。如下:新建的符号链接文件,大小为8k,比源文件小;且inode编号也与源文件不同,所以符号链接文件和源文件是两个不同的文件。
shaphicprb13137:~/test2 # ll
total 12
-rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
-rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
-rw-r----- 1 root root 93 Sep 11 16:53 test.txt
shaphicprb13137:~/test2 # ls -li
total 12
813503 -rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
813480 -rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
813506 -rw-r----- 1 root root 93 Sep 11 16:53 test.txt
shaphicprb13137:~/test2 # ln -s test.txt sl_test.txt
shaphicprb13137:~/test2 # ls -li
total 12
812857 lrwxrwxrwx 1 root root  8 Sep 11 17:09 sl_test.txt -> test.txt
813503 -rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
813480 -rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
813506 -rw-r----- 1 root root 93 Sep 11 16:53 test.txt
  • 硬链接会创建独立的虚拟文件,其中包含了原始文件的信息及位置。他们从根本上而言是同一个文件,其inode编号也是一致的。引用硬链接文件等同于引用了源文件。创建硬链接时使用ln命令,不需要加任何参数。如下:新创建的sl_test2.txt大小与源文件相同,inode编号也相同,两者其实是同一个文件。
shaphicprb13137:~/test2 # ls -li
total 12
812857 lrwxrwxrwx 1 root root  8 Sep 11 17:09 sl_test.txt -> test.txt
813503 -rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
813480 -rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
813506 -rw-r----- 1 root root 93 Sep 11 16:53 test.txt
shaphicprb13137:~/test2 # ln test.txt sl_test2.txt
shaphicprb13137:~/test2 # ls -li
total 16
813506 -rw-r----- 2 root root 93 Sep 11 16:53 sl_test2.txt
812857 lrwxrwxrwx 1 root root  8 Sep 11 17:09 sl_test.txt -> test.txt
813503 -rw-r----- 1 root root 21 Sep 11 16:38 test2.txt
813480 -rw-r----- 1 root root 21 Sep 11 16:38 test.bkp
813506 -rw-r----- 2 root root 93 Sep 11 16:53 test.txt

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值