User Tools

Site Tools


wiki:cpmv

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

wiki:cpmv [2015/11/11 10:44]
asa
wiki:cpmv [2016/08/02 14:47]
Line 1: Line 1:
-==== Moving and copying files and directories (chapter 4) ==== 
- 
-First, go into the ''​cs580''​ directory we created last time. 
-Before we start moving and copying files we should create one.  To do that we will use the command-line text editor ''​nano'',​ which is standard on any Linux/Unix system. 
-Let's create a new file called ''​my_new_file.txt'':​ 
- 
-<code bash> 
-$ nano my_new_file.txt 
-</​code>​ 
-Type some stuff and then save the file and exit the program. 
- 
-Now, to demonstrate why it's not a good idea to use spaces in file names, try the following: 
- 
-<code bash> 
-$ nano my new file.txt 
-</​code>​ 
-And type ctrl-x and observe what happens. 
- 
-Today we are going to use the following commands: 
- 
-  * **cp** - copy files and directories 
-  * **mv** - move/rename files and directories 
-  * **mkdir** - create a directory 
-  * **rmdir** - remove an empty directory 
-  * **rm** - remove files/​directories 
- 
-== Copying files == 
- 
-We already started using mkdir and rmdir, so we will focus on the three others. ​ And let's start with cp.  There are two forms for the cp command: 
- 
-<code bash> 
-  cp source_file target_file 
-  cp source_file ... target_directory 
-</​code>​ 
- 
-In the first form, cp copies the contents of the source_file to the target_file. 
-For example: 
-<code bash> 
-$ cp my_new_file.txt another_new_file.txt 
-</​code>​ 
-or 
-<code bash> 
-$ cp my_new_file.txt ../​yet_another_new_file.txt 
-</​code>​ 
-which copies the file under a new name into the parent directory of your current working directory. 
- 
-The second form of cp copies each each  
-source_file to the destination target_directory.  ​ 
-The names of the files themselves are not changed.  ​ 
-If cp detects an attempt to copy a file to itself, the copy will fail. 
-Now let's see some examples: 
-<code bash> 
-$ cp my_new_file.txt /home/asa 
-</​code>​ 
-which can also be written as ''​cp my_new_file.txt ~''​. 
- 
-You can also copy a file to the current working directory, abbreviated as ''​.'':​ 
-<code bash> 
-$ cp ../​yet_another_new_file.txt . 
-</​code>​ 
- 
-Using this form of the command you can copy multiple files at the same time: 
-<code bash> 
-$ cp my_new_file.txt another_new_file.txt /home/asa 
-</​code>​ 
- 
-cp has a lot of options. ​ Let's look at one of them - the ''​-R''​ option. ​ Try the following: ​ in your home directory type the command 
-<code bash> 
-$ cp -R cs580 copy_of_cs580 
-</​code>​ 
-What did it do? 
- 
- 
-== Removing files/​directories == 
- 
-Time to clean up a bit ... Let's remove the files from the ''​copy_of_cs580''​ directory and the directory itself. ​ We'll do that with the ''​rm''​ command. ​ You can remove individual files and then the directory itself: 
-<code bash> 
-$ rm copy_of_cs580/​my_new_file.txt 
-$ rmdir copy_of_cs580 
-</​code>​ 
-or you can use the ''​-R''​ option of rm: 
-<code bash> 
-$ rm -R copy_of_cs580 
-</​code>​ 
-Be very careful with "​rm"​. ​ Linux does not have a way of undoing its action. 
- 
-=== Renaming/​moving files/​directories === 
- 
-Like the cp command, the mv command has two general forms: 
- 
-<code bash> 
-  mv source target 
-  mv source ... directory 
-</​code>​ 
-Let's look at an example of the first one: 
-<code bash> 
-$ mv my_new_file.txt new_file.txt 
-</​code>​ 
-And the second one: 
-<code bash> 
-$ mv new_file .. 
-</​code>​ 
-The first one changes the name of the file, while the second moves the file to a different directory. 
- 
-While you can do all these things quite easily with a graphical file manager, doing it from the command line provides extra flexibility. 
-This flexibility comes from the use of wildcards. 
- 
-=== Wildcards === 
- 
-Wildcards enable you to select multiple files/​directories at once. 
-The most commonly used wildcard is the ''​*'',​ which matches any characters. 
-So, if we would like to see all the names of all files which end with ''​.txt''​ we would issue the following ls command: 
-<code bash> 
-$ ls *.txt 
-</​code>​ 
-and  
-<code bash> 
-$ ls m*.txt 
-</​code>​ 
-will list all files whose names begin with ''​m''​ and end with ''​.txt''​. 
- 
-Wildcards can be used in conjunction with any of the commands we looked at. 
-For example: 
-<code bash> 
-$ cp directory1/​* directory2 
-</​code>​ 
-will copy all the files from ''​directory1''​ to ''​directory2''​. 
- 
-There are many more wild cards that are recognized. ​ For example ''?''​ matches a single character, so ''​data???''​ will match file names that start with data and are followed by any three characters. ​ Chapter 4 of the book provides more information on this topic. 
- 
-Be especially careful when using wildcards with ''​rm'':​ first use the ''​ls''​ command with the wildcard to verify which files will be affected. 
-Consider for example the following command: 
-<code bash> 
-rm * .html  
-</​code>​ 
-It will remove all your files (and then complain that the file .html does not exist). 
  
wiki/cpmv.txt ยท Last modified: 2016/08/02 14:47 (external edit)