部分简单 shell 命令合集

2020-02-17  本文已影响0人  有情怀的程序猿

遍历目录

christine@server01:~/Documents$ pwd
/home/christine/Documents
christine@server01:~/Documents$ cd ../Downloads
christine@server01:~/Downloads$ pwd
/home/christine/Downloads
christine@server01:~/Downloads$

文件和目录列表

$ ls -l
total 48
drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Desktop
drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Documents
drwxr-xr-x 2 christine christine 4096 Apr 22 20:37 Downloads
-rw-r--r-- 1 christine christine 8980 Apr 22 13:36 examples.desktop
-rw-rw-r-- 1 christine christine    0 May 21 13:44 fall
-rw-rw-r-- 1 christine christine    0 May 21 13:44 fell
-rw-rw-r-- 1 christine christine    0 May 21 13:44 fill
-rw-rw-r-- 1 christine christine    0 May 21 13:44 full
drwxr-xr-x 2 christine christine 4096 May 21 11:39 Music
-rw-rw-r-- 1 christine christine    0 May 21 13:25 my_file
-rw-rw-r-- 1 christine christine    0 May 21 13:25 my_scrapt
$ ls -l my_scr?pt
-rw-rw-r-- 1 christine christine  0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$
$ ls -l my_s*t
-rw-rw-r-- 1 christine christine  0 May 21 13:25 my_scrapt
-rwxrw-r-- 1 christine christine 54 May 21 11:26 my_script
$

处理文件

$ touch test_one
$ ls -l test_one
-rw-rw-r-- 1 christine christine 0 May 21 14:17 test_one

$ cp  test_one test_two   // 复制 test_one  为 test_two
$ cp -i touch test_one test_two // 复制 test_one 为 test_two 且查询是否由同名文件
“cp: overwrite 'test_two'? n”

$ cp -i /etc/NetworkManager/NetworkManager.conf  . // 复制文件 /etc/NetworkManager/NetworkManager.conf  到当前文件目录
$ ls  f?ll
296717 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fall // 原文件名称 fall
$
$ mv fall  fzll  // 重命名文件名 fzll
$ ls  f?ll
296717 -rw-rw-r-- 1 christine christine 0 May 21 13:44 fzll // 重新查看文件,名称 fzll

// 移动目录
// fzll 原目录在 /home/christine/fzll 
现在执行
$ mv fzll  Pictures/
$
// 现在fzll 目录在  /home/christine/Pirctures中

$ rm -i fall //执行删除命令,且提示是否真要删除
rm: remove regular empty file 'fall'? y
$
$ ls -l fall
ls: cannot access fall: No such file or directory
$

处理目录

“有些命令(比如cp 命令)对文件和目录都有效,而有些只对目录有效。”

$ mkdir New_Dir
$ ls -ld New_Dir
drwxrwxr-x 2 christine christine 4096 May 22 09:48 New_Dir
$

系统创建了一个名为New_Dir的新目录。注意,新目录长列表是以d开头的。这表示New_Dir并不是文件,而是一个目录。

$ mkdir -p New_Dir/Sub_Dir/Under_Dir

rmdir 命令只删除空目录, 如果目录有文件则删除失败
rmdir 并没有-i 选项来询问是否要删除目录
如果目录内有文件需要先删除内部文件才可以删除目录

可以在整个非空目录上使用rm 命令。使用-r 选项使得命令可以向下进入目录,删除其中的文件,然后再删除目录本身
这种方法虽然可行,但很难用。注意,你依然要确认每个文件是否要被删除。如果该目录有很多个文件和子目录,这将非常琐碎。

$ ls -l My_Dir
total 0
-rw-rw-r-- 1 christine christine 0 May 22 10:02 another_file
$
$ rm -ri My_Dir
rm: descend into directory 'My_Dir'? y
rm: remove regular empty file 'My_Dir/another_file'? y
rm: remove directory 'My_Dir'? y
$
$ ls -l My_Dir
ls: cannot access My_Dir: No such file or directory
$

查看文件内容

$ file my_file
my_file: ASCII text
$
$ file New_Dir
New_Dir: directory

file 命令不仅能确定文件中包含的文本信息,还能确定该文本文件的字符编码,ASCII
而且还可以作为一种区分目录的方法

查看整个文件
$ cat test1
hello

This is a test file.


That we'll use to       test the cat command.
$

cat 命令的主要缺陷是:一旦运行,你就无法控制后面的操作,
如果文件很大则一闪而过。
所以:
可以使用 more / less 命令,可以实现上下翻页

但是如果一个很大文件我们要的信息在文件结尾 / 中间部分,怎么搞,翻页不翻死了?

tail 功能一样的是 head

上一篇 下一篇

猜你喜欢

热点阅读