学习 Unix 常用命令

第一个是 man 命令,作用是:"Display system documentation",我猜测英文是 manual 的缩写。通过这个命令,我们能了解接下来要学习的命令的文档。

ls, pwd, cd 的基本用法我觉得不需要额外说明。

cp。

cp命令的复杂性源于文件系统的复杂性。常遇到的情况有:如何处理软链接文件?

如果目标文件存在,怎么办?如果目标文件存在,但是打不开,怎么处理?

source file 是一个文件夹的话,要复制多少,是复制整个文件夹,还是部分?

这些都能通过 cp 的参数来解决。

对于已存在的目标文件

-f 在权限范围内,强制删除然后新建并复制过去;

-i 提示用户,并等待用户选择

-n 不覆盖已存在文件,并且不会提示

软链接

-L    If the -R option is specified, all symbolic links are followed.

这个解释少了半截。搜索了一下,完整的意思是这样““Follows the links in the source to find the file to copy instead of copying the links ””

-P    If the -R option is specified, no symbolic links are followed.  This is the default.

默认情况下,只复制软链接而不复制它所对应的文件

源文件为文件夹

-R    If source_file designates a directory, cp copies the directory and

the entire subtree connected at that point.  If the source_file

ends in a /, the contents of the directory are copied rather than

the directory itself.  This option also causes symbolic links to be

copied, rather than indirected through, and for cp to create spe-

cial files rather than copying them as normal files.  Created

directories have the same mode as the corresponding source direc-

tory, unmodified by the process‘ umask.

In -R mode, cp will continue copying even if errors are detected.

Note that cp copies hard-linked files as separate files.  If you

need to preserve hard links, consider using tar(1), cpio(1), or

pax(1) instead.

这里有一个比较隐蔽的选项,如果源文件是文件夹,并且它的路径以 ‘/‘ 结尾,就复制该文件夹下的所有文件过去(不放在该文件夹中)而不是复制该文件夹过去。

我们做一下测试

mkdir source_dir target_dir
$ touch source_dir/file1 source_dir/file2
$ ls source_dir
file1	file2
$ ls target_dir
$ cp -R source_dir target_dir/
$ ls target_dir/
source_dir
$ cp -R source_dir/ target_dir/
$ ls target_dir/
file1		file2		source_dir

这些参数可以组合使用,组合使用后也有问题,如果参数的作用冲突了,你还要弄清冲突后的效果。这里我暂时不深入了。

rm

rm 与 cp 类似,也存在文件夹、软链接的问题。

不带参数的 rm 只能删除非目录类型的文件。

删除文件夹

-d          Attempt to remove directories as well as other types of files.

尝试了一下,发现不能移除非空文件夹

如果要移除非空文件夹,使用 -R

软链接

The rm utility removes symbolic links, not the files referenced by the links.

mkdir

创建文件夹。

默认情况下,如果要创建的文件夹的中间路径有不存在的,那么会报错;

如果有 -p 选项,那么会自动创建中间不存在的文件夹;

ln

man ln 的说明看的有点糊涂,于是找到了 ln (Unix) 上的解释

通过链接文件,不同的文件名可以指向同一个文件。

ln 可以创建两种类型的链接文件:

  1. 符号链接,也称软链接,这是指向另一个不同路径文件的一个符号路径。
  2. 硬链接,这是一个存储了链接建立时它所指向文件的实际数据的文件副本。

从以下命令示例可看出两种链接文件的区别:

$ echo ‘文件内容‘ > oringinal.file
$ ln oringinal.file hardlink.file
$ ln -s oringinal.file softlink.file
$ cat softlink.file
文件内容
$ rm oringinal.file
$ cat softlink.file
cat: softlink.file: 没有那个文件或目录
$ cat hardlink.file
文件内容

原始文件被删除后,符号链接将失效,访问软链接时,会提示找不到文件,但硬链接文件还在,而且还保存有原始文件的内容。

硬链接与 cp 有什么不一样?  

要理解这个,先要理解 inodes。这里 http://teaching.idallen.com/cst8207/12f/notes/460_links_and_inodes.html 讲的很详细。看完就就会理解下面的结论。

Thus, on Unix, a file can have many names, even across different directories. You may use any of the several names of a file to find the inode for the file. Unix calls these names "pointers" or "links" to the file.

Note that the "rm" command does not delete a file - it only deletes a name-inode map for a file. Only when all the name-inode maps are gone does the actual file data space get reclaimed.

读完上面那篇文章,我们能够知道,如果更改硬链接的文件,那么原始文件、软链接的文件都会变化,因为它们都指向相同的 inodes。但是复制出的文件就不会变化,因为是不同的 inodes 了。代码如下:

$ echo ‘file content‘ > original.file
$ ln original.file hardlink.file
$ ln -s original.file softlink.file
$ cp original.file copied_original.file
$ rm original.file
$ ln hardlink.file original.file
# 更改文件
$ vim original.file
# 更改后的文件内容
$ cat original.file
file content
updated!!!
$ cat hardlink.file
file content
updated!!!
$ cat softlink.file
file content
updated!!!
# 使用 cp 复制出来的没有变化
$ cat copied_original.file
file content

  

mv

移动文件(夹)。可以看成是 cp 与 rm 的结合

rm -f destination_path && cp -pRP source_file destination && rm -rf source_file
cp -pRP

从源文件复制过去,保留各种属性信息,复制软链接而不是它的源文件,如果是文件夹,复制整个文件夹过去。

touch

改变文件的访问和修改时间。如果文件不存在,那么创建这个文件,文件权限为默认权限。

cat

cat -- concatenate and print files

The command:

cat file1 file2 > file3

will sequentially print the contents of file1 and file2 to the file file3, truncating file3 if it already exists.  See the manual page for your shell (i.e., sh(1)) for more

information on redirection.

The command:

cat file1 - file2 - file3

will print the contents of file1, print data it receives from the standard input until it receives an EOF (`^D‘) character, print the contents of file2, read and output contents

of the standard input again, then finally output the contents of file3.  Note that if the standard input referred to a file, the second dash on the command-line would have no

effect, since the entire contents of the file would have already been read and printed by cat when it encountered the first `-‘ operand.

cat file1 file2 > file3 会将 file1, file2 的内容联合起来,写入到 file3。file3 的内容会丢失。

cat file1 - file2 - file3 会打印输入的,直到 EOF 为止。不知道这个的作用是什么。

时间: 2024-08-07 04:31:44

学习 Unix 常用命令的相关文章

c/c++unix/linux基础学习笔记-常用命令和vi的使用

linux 基本命令的使用-命令在ubuntu下面执行,有些命令通用其他linux,有些不通用. 多条命令间用;号隔开,回车后可以一起执行. clear-前屏,pwd显示当前目录,cd跳转目录. sudo [命令]  -ubuntu 下以管理员身份运行命令. 一般情况下,运行当前目录下的程序,要用 ./文件名 执行. 查看当前shell名称:ps 进入另外一个shell,直接输入shell名称:ksh/tcsh/sh/bash,退出一个shell用:exit. 切换shell命令,如:exec

工作中用到的 Linux/Unix 常用命令

LINUX平时工作中用到的常用命令 :       scp是有Security的文件copy,基于ssh登录.操作起来比较方便,比如要把当前一个文件copy到远程另外一台主机上,可以如下命令. scp /home/1.gif [email protected]:/home/root 然后会提示你输入另外那台172.19.2.75主机的root用户的登录密码,接着就开始cp和ungzip了 如果想反过来操作,把文件从远程主机copy到当前系统,也很简单: scp [email protected]

一张图学习vim常用命令

一张图学习vim常用命令

Linux_学习_01_常用命令大全

二.参考资料 1.[笔记]Linux命令行大全 2. Linux命令大全(手册)_Linux常用命令行实例详解_Linux命令学习手册 3.Linux常用命令大全 原文地址:https://www.cnblogs.com/shirui/p/8353439.html

Linux学习笔记——常用命令(一)

Linux分区的四个基本步骤: 1)分区:硬盘划分为逻辑分区 2)格式化逻辑分区(写入文件系统) 3)分区设备文件名:给每个分区定义设备文件名 4)挂载点:给每个分区分配挂载点 注意事项: 1)必须分区: /   /boot  /swap 2)一块硬盘最多4个分区,最多1个扩展分区,扩展分区又可以包含多个逻辑分区 设置密码原则: 1)复杂性 2)易记性 3)时效性 目录usr(unlix software resource) 防火墙:用来过滤,制定一系列的规则(IP.MAC.端口等) Linux

Redis 学习之常用命令及安全机制

该文使用centos6.5 64位    redis3.2.8 一.redis常用命令 键值常用命令: 1. keys 返回满足pattern的所有key. 127.0.0.1:6379> keys my* 127.0.0.1:6379> keys * 2.exits 确认key是否存在. 返回1表示存在 0表示不存在 127.0.0.1:6379> exists name 3.del :删除一个键 返回1:删除成功 0:失败 127.0.0.1:6379> del name (i

ORACLE学习03-SQLPLUS常用命令和数据类型

一.数据库基本操作 1,创建用户 SQL> create user 用户名 identified by 密码; 2,超级用户system给新用户权限 SQL> grant connect , resource to mike; 授权 连接权限 增删改查权限 3,新用户登录,进入新用户的空间 SQL> connect mike/m111; 4,显示当前用户 SQL>show user; 5,建表,添加数据,增删改查. 6,登录sqlplus >sqlplus 登录名/密码@12

Redis学习(5)-常用命令

Redis常用命令Redis提供了丰富的命令对数据库和各种数据库类型进行操作,这些命令可以在Linux终端使用1.键值相关命令2.服务器相关命令 键值相关命令 Keys pattern 例如:keys *返回满足给定pattern的所有key exists 确认一个key是否存在 del删除一个key expire设置一个key的过期时间例如:expire addr 10  (时间10 s)ttl addr(使用ttl,获取addr的有效时长)返回-1,已经过期 move将当前数据库中的key转

Git学习 --> 个人常用命令add,commit以及push

Git命令行配置1 安装Github2 安装msysgit3 要配置用户名和油箱  git config --global user.name <用户名> 我的命令就是:git config --global user.name mchdbagh  git config --global user.email <油箱> 我的命令就是:git config --global mc[email protected]4 验证有没有连接上remote远程服务器ssh -T [email p