http://linuxcommand.org/lc3_lts0050.php

This lesson will introduce you to the following commands:

  • cp - copy files and directories
  • mv - move or rename files and directories
  • rm - remove files and directories
  • mkdir - create directories

These four commands are among the most frequently used Linux commands. They are the basic commands for manipulating both files and directories.

Now, to be frank, some of the tasks performed by these commands are more easily done with a graphical file manager. With a file manager, you can drag and drop a file from one directory to another, cut and paste files, delete files, etc. So why use these old command line programs?

The answer is power and flexibility. While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. For example, how would you copy all the HTML files from one directory to another, but only copy files that did not exist in the destination directory or were newer than the versions in the destination directory? Pretty hard with with a file manager. Pretty easy with the command line:

[me@linuxbox me]$ cp -u *.html destination

Wildcards

Before I begin with our commands, I want to talk about a shell feature that makes these commands so powerful. Since the shell uses filenames so much, it provides special characters to help you rapidly specify groups of filenames. These special characters are called wildcards. Wildcards allow you to select filenames based on patterns of characters. The table below lists the wildcards and what they select:

Summary of wildcards and their meanings
Wildcard Meaning

*

Matches any characters

?

Matches any single character

[characters]

Matches any character that is a member of the set characters. The set of characters may also be expressed as a POSIX character class such as one of the following:

POSIX Character Classes
[:alnum:] Alphanumeric characters
[:alpha:] Alphabetic characters
[:digit:] Numerals
[:upper:] Uppercase alphabetic characters
[:lower:] Lowercase alphabetic characters

[!characters]

Matches any character that is not a member of the set characters

Using wildcards, it is possible to construct very sophisticated selection criteria for filenames. Here are some examples of patterns and what they match:

Examples of wildcard matching
Pattern Matches
*

All filenames

g*

All filenames that begin with the character "g"

b*.txt

All filenames that begin with the character "b" and end with the characters ".txt"

Data???

Any filename that begins with the characters "Data" followed by exactly 3 more characters

[abc]*

Any filename that begins with "a" or "b" or "c" followed by any other characters

[[:upper:]]*

Any filename that begins with an uppercase letter. This is an example of a character class.

BACKUP.[[:digit:]][[:digit:]]

Another example of character classes. This pattern matches any filename that begins with the characters "BACKUP." followed by exactly two numerals.

*[![:lower:]]

Any filename that does not end with a lowercase letter.

You can use wildcards with any command that accepts filename arguments.

cp

The cp program copies files and directories. In its simplest form, it copies a single file:

[me@linuxbox me]$ cp file1 file2

It can also be used to copy multiple files (and/or directories) to a different directory:

[me@linuxbox me]$ cp file... directory

A note on notation: ... signifies that an item can be repeated one or more times.

Other useful examples of cp and its options include:

Examples of the cp command
Command Results
cp file1 file2

Copies the contents of file1 into file2. If file2 does not exist, it is created; otherwise, file2 is silently overwritten with the contents of file1.

cp -i file1 file2

Like above however, since the "-i" (interactive) option is specified, if file2 exists, the user is prompted before it is overwritten with the contents offile1.

cp file1 dir1

Copy the contents of file1 (into a file named file1) inside of directory dir1.

cp -R dir1 dir2

Copy the contents of the directory dir1. If directory dir2 does not exist, it is created. Otherwise, it creates a directory named dir1 within directorydir2.

mv

The mv command moves or renames files and directories depending on how it is used. It will either move one or more files to a different directory, or it will rename a file or directory. To rename a file, it is used like this:

[me@linuxbox me]$ mv filename1 filename2

To move files (and/or directories) to a different directory:

[me@linuxbox me]$ mv file... directory

Examples of mv and its options include:

Examples of the mv command
Command Results
mv file1 file2

If file2 does not exist, then file1 is renamed file2If file2 exists, its contents are silently replaced with the contents of file1.

mv -i file1 file2

Like above however, since the "-i" (interactive) option is specified, if file2 exists, the user is prompted before it is overwritten with the contents of file1.

mv file1 file2 file3 dir1

The files file1, file2, file3 are moved to directory dir1. If dir1 does not exist, mv will exit with an error.

mv dir1 dir2

If dir2 does not exist, then dir1 is renamed dir2. If dir2 exists, the directory dir1 is moved within directory dir2.

rm

The rm command removes (deletes) files and directories.

[me@linuxbox me]$ rm file...

It can also be used to delete directories:

[me@linuxbox me]$ rm -r directory...

Examples of rm and its options include:

Examples of the rm command
Command Results
rm file1 file2

Delete file1 and file2.

rm -i file1 file2

Like above however, since the "-i" (interactive) option is specified, the user is prompted before each file is deleted.

rm -r dir1 dir2

Directories dir1 and dir2 are deleted along with all of their contents.

Be careful with rm!

Linux does not have an undelete command. Once you delete something with rm, it's gone. You can inflict terrific damage on your system with rm if you are not careful, particularly with wildcards.

Before you use rm with wildcards, try this helpful trick: construct your command using ls instead. By doing this, you can see the effect of your wildcards before you delete files. After you have tested your command with ls, recall the command with the up-arrow key and then substitute rm for ls in the command.

mkdir

The mkdir command is used to create directories. To use it, you simply type:

[me@linuxbox me]$ mkdir directory...

Using Commands With Wildcards

Since the commands we have covered here accept multiple file and directories names as arguments, you can use wildcards to specify them. Here are a few examples:

Command examples using wildcards
Command Results
cp *.txt text_files

Copy all files in the current working directory with names ending with the characters ".txt" to an existing directory named text_files.

mv my_dir ../*.bak my_new_dir

Move the subdirectory my_dir and all the files ending in ".bak" in the current working directory's parent directory to an existing directory named my_new_dir.

rm *~

Delete all files in the current working directory that end with the character "~". Some applications create backup files using this naming scheme. Using this command will clean them out of a directory.

Previous | Contents | Top | Next

 

© 2000-2015, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.

Linux® is a registered trademark of Linus Torvalds.

Manipulating Files的更多相关文章

  1. Java NIO Files

    Java NIO Files Files.exists() Files.createDirectory() Files.copy() Overwriting Existing Files Files. ...

  2. Vault 不同版本的API的异同

    大家知道,Autodesk Vault 2014有几个版本,依次为( Basic, Workgroup, Professional),不同版本的功能不相同,关于Vault产品功能的不同之处可以在Vau ...

  3. 《The Linux Command Line》 读书笔记01 基本命令介绍

    <The Linux Command Line> 读书笔记01 基本命令介绍 1. What is the Shell? The Shell is a program that takes ...

  4. Java fundamentals of basic IO

    IO is a problem difficult to handle in various of systems because it  always becomes a bottleneck in ...

  5. RFC-TCP

    RFC: 793 TRANSMISSION CONTROL PROTOCOL DARPA INTERNET PROGRAM PROTOCOL SPECIFICATION September 1981 ...

  6. QtCore Module's Classes

    Qt Core C++ Classes Provides core non-GUI functionality. More... Reference These are links to the AP ...

  7. SHFileOperation的用法

    //删除文件或者文件夹bool DeleteFile(char * lpszPath){SHFILEOPSTRUCT FileOp={0};FileOp.fFlags = FOF_ALLOWUNDO ...

  8. SVN Client API的.net 接口 SharpSvn介紹 Checkout操作实例

    Subversion是一個文件版本管理工具, 廣泛的被大家採用來作為源代碼版本管理. 已有的工具不管是其自帶的命令行工具還是Windows UI的tortoiseSVN等還是很方便實用的, 但是如果想 ...

  9. 深入代码详谈irqbalance【转】

    转自:http://blog.csdn.net/whrszzc/article/details/50533866 版权声明:本文为博主原创文章,未经博主允许不得转载. 深入代码详谈irqbalance ...

随机推荐

  1. Qt Qwdget 汽车仪表知识点拆解3 进度条编写

    先贴上效果图,注意,没有写逻辑,都是乱动的 这篇我来说说左侧的这个进度条的实现原理,其实更简单,哈哈哈 有一个大的widget,根据素材,我放了10个label 剩下的就是写一个函数,根据数据的不同, ...

  2. 名字管理系统demo

    # 名字管理系统demo # 打印功能提示 print('欢迎使用名字管理系统v6.6.6') print('1:添加一个名字') print('2:删除一个名字') print('3:修改一个名字' ...

  3. 为什么mysqld启动报错

    在一台ubuntu测试机器上启动一个mysql实例,本来应该是一件很简单的事情, 启动的时候却报错了:   mysqld_safe --defaults-file=/etc/mysql/my3307. ...

  4. 官方文档 恢复备份指南五 Configuring the RMAN Environment

    本章内容: Configuring the Environment for RMAN Backups    配置RMAN环境 Configuring RMAN to Make Backups to a ...

  5. 第十四次ScrumMeeting会议

    第十三次Scrum Meeting 时间:2017/12/2 地点:咖啡馆 人员:策划组美工组 目前工作情况 名字 完成的工作 计划工作 蔡帜 科技树策划设计,科技图鉴蓝图设计 员工方面细节设定 游心 ...

  6. direct.h头文件(对目录操作)

    chdir()改变当前目录的函数原形:int chdir(const char *path)功能:把由path指定的目录改为当前目录.path参数中可以指定驱动器号,如“a:\\ddd”, 但只是改变 ...

  7. postman工具中如何提取接口的返回值

    提取接口返回值   当返回值是返回JSON时 1.let json = JSON.parse(responseBody); // responseBody是包含整个返回内容的字符串 提取某字段的值: ...

  8. PHP判断类型的方法

    1.gettype():获取变量类型 2.is_array():判断变量类型是否为数组类型 3.is_double():判断变量类型是否为倍浮点类型 4.is_float():判断变量类型是否为浮点类 ...

  9. pptp协议的工作原理

    我的工作机是A,通信网卡是Aeth0, Appp0: 然后我的云主机是B, 通信的网卡是Beth0, Bppp0: 在网卡Bppp0上会不断地很清晰的数据包: 16:40:39.522917 IP 6 ...

  10. Java中输入输出流

    InputStream:所有字节输入流的所有类的超类. read(byte[] b)从输入流中读取一定数量的字节,并将其存储在缓冲数组b中 reset()将此流重新定位到最后一次对此流调用mark方法 ...