文件和目录


文档摘要

文件和目录 [pwd]() [cd]() [clear]() [ls]() [mkdir]() [touch]() [rm]() [cp]() [mv]() [rename]() [ln]() [tar 和 gzip]() cd 更改当前 shell 工作目录 pwd cd / 或者 cd 或者 cd 将会进入由 HOME 环境变量指定的目录(通常设置为用户的主目录) clear 清除终端屏幕 ls 列出目录内容 mkdir 创建目录 进一步阅读 关于 mkdir 的问答 - unix stackexchange 关于 mkdir 的问答 - stackoverflow unix.

文件和目录

cd

更改当前 shell 工作目录

pwd

$ pwd /home/learnbyexample $ # providing an absolute path as argument $ cd /etc $ pwd /etc $ # to go back to previous working directory $ # if there's a directory named '-', use './-' to go that directory $ cd - /home/learnbyexample $ pwd /home/learnbyexample Relative paths are well, relative to current working directory . refers to current directory .. refers to directory one hierarchy above ../.. refers to directory two hierarchies above and so on $ pwd /home/learnbyexample $ # go to directory one hierarchy above $ cd .. $ pwd /home $ # go to directory 'learnbyexample' present in current directory $ # './' is optional in this case $ cd ./learnbyexample $ pwd /home/learnbyexample $ # go to directory two hierarchies above $ cd ../.. $ pwd /

cd ~/ 或者 cd ~ 或者 cd 将会进入由 HOME 环境变量指定的目录(通常设置为用户的主目录)

$ pwd / $ echo "$HOME" /home/learnbyexample $ cd $ pwd /home/learnbyexample

clear

清除终端屏幕

ls

列出目录内容

mkdir

创建目录

$ # one or more absolute/relative paths can be given to create directories $ mkdir reports 'low power adders' $ # listing can be confusing when filename contains characters like space $ ls low power adders reports $ ls -1 low power adders reports use -p option to create multiple directory hierarchies in one go it is also useful in scripts to create a directory without having to check if it already exists special variable $? gives exit status of last executed command 0 indicates success and other values indicate some kind of failure see documentation of respective commands for details $ mkdir reports mkdir: cannot create directory ‘reports’: File exists $ echo $? 1 $ # when -p is used, mkdir won't give an error if directory already exists $ mkdir -p reports $ echo $? 0 $ # error because 'a/b' doesn't exist $ mkdir a/b/c mkdir: cannot create directory ‘a/b/c’: No such file or directory $ # with -p, any non-existing directory will be created as well $ mkdir -p a/b/c $ ls -1R a a: b a/b: c a/b/c:

进一步阅读

touch

通常使用文本编辑器或命令重定向输出到文件来创建文件。但有时,例如为了测试文件重命名,创建空文件会很有用。touch 命令主要用于更改文件的时间戳。

$ touch test.txt $ ls -1F a/ test.txt low power adders/ reports/

rm

删除文件和目录

$ ls a ip.txt low power adders reports $ rm ip.txt $ ls a low power adders reports $ rm reports rm: cannot remove 'reports': Is a directory $ rm -r reports $ ls a low power adders $ # to remove only empty directory, same as 'rmdir' command $ rm -d a rm: cannot remove 'a': Directory not empty typos like misplaced space, wrong glob, etc could wipe out files not intended for deletion apart from having backups and snapshots, one could take some mitigating steps using -i option to interactively delete each file using echo as a dry run to see how the glob expands using a trash command (see links below) instead of rm $ rm -ri 'low power adders' rm: remove directory 'low power adders'? n $ ls a low power adders $ rm -ri a rm: descend into directory 'a'? y rm: descend into directory 'a/b'? y rm: remove directory 'a/b/c'? y rm: remove directory 'a/b'? y rm: remove directory 'a'? y $ ls low power adders

cp

复制文件和目录

$ # when destination is a directory, specified sources are placed inside that directory $ # recall that . is a relative path referring to current directory $ cp /usr/share/dict/words . $ ls low power adders words $ cp /usr/share/dict . cp: omitting directory '/usr/share/dict' $ cp -r /usr/share/dict . $ ls -1F dict/ low power adders/ words often, we want to copy for the purpose of modifying it in such cases, a different name can be given while specifying the destination if the destination filename already exists, it will be overwritten (see options -i and -n to avoid this) $ cp /usr/share/dict/words words_ref.txt $ cp -r /usr/share/dict word_lists $ ls -1F dict/ low power adders/ word_lists/ words words_ref.txt multiple files and directories can be copied at once if the destination is a directoryusing -t option, one could specify destination directory first followed by sources (this is helpful with find command and other places) $ mkdir bkp_dot_files $ # here, ~ will get expanded to user's home directory $ cp ~/.bashrc ~/.bash_profile bkp_dot_files/ $ ls -A bkp_dot_files .bash_profile .bashrc\
  • 查看 man cp 和 info cp 以获取更多选项和完整文档
  • 一些值得注意的选项有
    • -u 只在源文件比目标文件新或者目标文件不存在时才复制
    • -b 和 --backup 如果目标位置已经存在同名文件,则提供备份选项
    • --preserve 选项用于复制文件时保留源文件的属性,如时间戳

进一步阅读

mv

移动(重命名)文件

$ ls bkp_dot_files dict low power adders word_lists words words_ref.txt $ mkdir backups $ mv bkp_dot_files/ backups/ $ ls -F backups/ dict/ low power adders/ word_lists/ words words_ref.txt $ ls -F backups/ bkp_dot_files/ $ mv dict words backups/ $ ls -F backups/ low power adders/ word_lists/ words_ref.txt $ ls -F backups/ bkp_dot_files/ dict/ words like cp command, for single file/directory one can provide a different destination name so, when source and destination has same parent directory, mv acts as renaming command $ mv backups/bkp_dot_files backups/dot_files $ ls -F backups/ dict/ dot_files/ words

进一步阅读

rename

批量重命名文件

$ ls backups low power adders word_lists words_ref.txt $ # here, the * glob will expand to all non-hidden files in current directory $ # -n option is for dry run, to see changes before actually renaming files $ # s/ /_/g means replace all space characters with _ character $ rename -n 's/ /_/g' * rename(low power adders, low_power_adders) $ rename 's/ /_/g' * $ ls backups low_power_adders word_lists words_ref.txt

进一步阅读

ln

创建文件之间的链接

链接有两种类型 - 符号链接和硬链接。符号链接类似于指向另一个文件或目录的指针/快捷方式。如果原始文件被删除或移动到其他位置,符号链接将不再有效。如果符号链接被移动到其他位置,它仍然有效,前提是它是通过绝对路径创建的(对于相对路径,取决于是否在同一位置有同名文件)。符号链接文件有自己的 inode、权限、时间戳等。大多数命令在给出原始文件或符号链接作为命令行参数时表现相同,请查阅其文档以获取详细信息。

$ # similar to cp, a different name can be specified if needed $ ln -s /usr/share/dict/words . $ ls -F words@ $ # to know which file the link points to $ ls -l words lrwxrwxrwx 1 learnbyexample eg 21 Jul 9 13:41 words -> /usr/share/dict/words $ readlink words /usr/share/dict/words $ # the linked file may be another link $ # use -f option to get original file $ readlink -f words /usr/share/dict/english

硬链接只能指向另一个文件(不能是目录,并且限制在同一文件系统内)。. 和 .. 特殊目录是例外,一旦创建了硬链接,它们就会自动创建,没有区别,除了不同的文件名/位置 - 它们具有相同的 inode、权限、时间戳等。即使删除了其他所有硬链接,任何硬链接都会继续工作。如果将硬链接移动到另一个位置,链接仍然保持同步 - 一个文件的任何更改都会反映在其他所有链接上。

$ touch foo.txt $ ln foo.txt baz.txt $ # the -i option gives inode $ ls -1i foo.txt baz.txt 649140 baz.txt 649140 foo.txt

进一步阅读

tar 和 gzip

tar 是一个归档工具,首先我们来看一个从多个输入文件创建单个归档文件的例子。请注意,所创建的归档文件是一个新文件,不会覆盖输入文件。

$ ls -F backups/ low_power_adders/ word_lists/ words_ref.txt $ # -c option creates a new archive, existing archive will be overwritten $ # -f option allows to specify name of archive to be created $ # rest of the arguments are the files to be archived $ tar -cf bkp_words.tar word_lists words_ref.txt $ ls -F backups/ bkp_words.tar low_power_adders/ word_lists/ words_ref.txt $ ls -sh bkp_words.tar 2.3M bkp_words.tar once we have an archive, we can compress it using gzip this will replace the archive file with compressed version, adding a .gz suffix $ gzip bkp_words.tar $ ls -F backups/ bkp_words.tar.gz low_power_adders/ word_lists/ words_ref.txt $ ls -sh bkp_words.tar.gz 652K bkp_words.tar.gz to uncompress, use gunzip or gzip -d this will replace the compressed version with the uncompressed archive file $ gunzip bkp_words.tar.gz $ ls -F backups/ bkp_words.tar low_power_adders/ word_lists/ words_ref.txt $ ls -sh bkp_words.tar 2.3M bkp_words.tar to extract the original files from archive, use -x option $ mkdir test_extract $ mv bkp_words.tar test_extract/ $ cd test_extract/ $ ls bkp_words.tar $ tar -xf bkp_words.tar $ ls -F bkp_words.tar word_lists/ words_ref.txt $ cd .. $ rm -r test_extract/ - the GNU version of tar supports compressing/uncompressing options as well $ ls -F backups/ low_power_adders/ word_lists/ words_ref.txt $ # -z option gives same compression as gzip command $ # reverse would be: tar -zxf bkp_words.tar.gz $ tar -zcf bkp_words.tar.gz word_lists words_ref.txt $ ls -sh bkp_words.tar.gz 652K bkp_words.tar.gz
  • 对于各种需求有许多选项,详见文档
    • -v 用于详细模式
    • -r 用于向归档中追加文件
    • -t 用于列出归档内容
    • --exclude= 用于指定要忽略归档的文件
    • -j 和 -J 用于使用 bzip2 或 xz 压缩技术,而不是 -z 使用 gzip
  • 有一些以 z 开头的命令用于处理压缩文件
    • zcat 用于显示压缩文件的内容
    • zless 用于分屏显示压缩文件的内容
    • zgrep 用于搜索压缩文件等...

进一步阅读

声明:
本文件灏天文库团队进行了翻译。尽管我们力求准确,但请注意,翻译可能包含错误或不准确之处。原文档以其原始语言为准。我们不对因使用此翻译而产生的任何误解或误译负责。


发布者: 作者: 转发
评论区 (0)
U