Sunday, 9 April 2017

Linux Tutorial : Working with Files and Directories Part III


Copying Files, Moving Files, Delete File


The cp command is used to copy files. It requires that you specify a source and a destination. The structure of the command is as follows:
cp [source] [destination]
The source is the file you wish to copy. The destination is where you want the copy to be located. When successful, the cp command will not have any output (no news is good news). The following command will copy the /etc/hosts file to your home directory:
sysadmin@localhost:~$ cp /etc/hosts ~                                     
sysadmin@localhost:~$ ls                                                  
Desktop    Downloads  Pictures  Templates  hosts                          
Documents  Music      Public    Videos                                    
sysadmin@localhost:~$
Remember: The ~ character represents your home directory.

1 Verbose Mode

The -v option will cause the cp command to produce output if successful. The -v option stands for verbose:
sysadmin@localhost:~$ cp -v /etc/hosts ~                              
`/etc/hosts' -> `/home/sysadmin/hosts'                                 
sysadmin@localhost:~$
When the destination is a directory, the resulting new file will have the same name as the original file. If you want the new file to have a different name, you must provide the new name as part of the destination:
sysadmin@localhost:~$ cp /etc/hosts ~/hosts.copy                      
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  hosts                       
Documents  Music      Public    Videos     hosts.copy                  
sysadmin@localhost:~$

2 Avoid Overwriting Data

The cp command can be destructive to existing data if the destination file already exists. In the case where the destination file exists , the cp command will overwrite the existing file's contents with the contents of the source file. To illustrate this potential problem, first a new file is created in the sysadmin home directory by copying an existing file:
sysadmin@localhost:~$ cp /etc/skel/.bash_logout ~/example.txt 
sysadmin@localhost:~$
View the output of the ls command to see the file and view the contents of the file using the more command:
sysadmin@localhost:~$ cp /etc/skel/.bash_logout ~/example.txt
sysadmin@localhost:~$ ls -l example.txt
-rw-rw-r--. 1 sysadmin sysadmin 18 Sep 21 15:56 example.txt
sysadmin@localhost:~$ more example.txt
# ~/.bash_logout: executed by bash(1) when login shell exits.

sysadmin@localhost:~$ cp -i /etc/hosts ~/example.txt
cp: overwrite `/home/sysadmin/example.txt'? n
sysadmin@localhost:~$ ls -l example.txt
-rw-rw-r--. 1 sysadmin sysadmin 18 Sep 21 15:56 example.txt
sysadmin@localhost:~$ more example.txt
# ~/.bash_logout: executed by bash(1) when login shell exits.

sysadmin@localhost:~$
In the next example, you will see that the cp command destroys the original contents of the example.txt file. Notice that after the cp command is complete, the size of the file is different (158 bytes rather than 18) from the original and the contents are different as well:
sysadmin@localhost:~$ cp /etc/hosts ~/example.txt
sysadmin@localhost:~$ ls -l example.txt
-rw-rw-r--. 1 sysadmin sysadmin 158 Sep 21 14:11 example.txt
sysadmin@localhost:~$ cat example.txt
127.0.0.1  localhost localhost.localdomain localhost4 localhost4.localdomain4
::1        localhost localhost.localdomain localhost6 localhost6.localdomain6
sysadmin@localhost:~$
There are two options that can be used to safeguard against accidental overwrites. With the -i (interactive) option, the cp will prompt before overwriting a file. The following example will demonstrate this option, first restoring the content of the original file:
sysadmin@localhost:~$ cp /etc/skel/.bash_logout ~/example.txt          
sysadmin@localhost:~$ ls -l example.txt                                
-rw-r--r-- 1 sysadmin sysadmin 18 Sep 21 15:56 example.txt           
sysadmin@localhost:~$ more example.txt                                 
# ~/.bash_logout: executed by bash(1) when login shell exits.         

sysadmin@localhost:~$ cp -i /etc/hosts ~/example.txt                   
cp: overwrite `/home/sysadmin/example.txt'? n                          
sysadmin@localhost:~$ ls -l example.txt                                
-rw-r--r-- 1 sysadmin sysadmin 18 Sep  21 15:56 example.txt            
sysadmin@localhost:~$ more example.txt                                
# ~/.bash_logout: executed by bash(1) when login shell exits.          

sysadmin@localhost:~$
Notice that since the value of n (no) was given when prompted to overwrite the file, no changes were made to the file. If a value of y (yes) was given, then the copy process would have taken place.
The -i option requires you to answer y or n for every copy that could end up overwriting an existing file's contents. This can be tedious when a bunch of overwrites could occur, such as the example demonstrated below:
sysadmin@localhost:~$ cp -i /etc/skel/.* ~                             
cp: omitting directory `/etc/skel/.'                                   
cp: omitting directory `/etc/skel/..'                                  
cp: overwrite `/home/sysadmin/.bash_logout'? n                         
cp: overwrite `/home/sysadmin/.bashrc'? n                              
cp: overwrite `/home/sysadmin/.profile'? n                            
cp: overwrite `/home/sysadmin/.selected_editor'? n                     
sysadmin@localhost:~$
As you can see from the example above, the cp command tried to overwrite four existing files, forcing the user to answer three prompts. If this situation happened for 100 files, it could become very annoying, very quickly.
If you want to automatically answer n to each prompt, use the -n option. It essentially stands for "no rewrite”.

3 Copying Directories

In a previous example, error messages were given when the cp command attempted to copy directories:
sysadmin@localhost:~$ cp -i /etc/skel/.* ~                             
cp: omitting directory `/etc/skel/.'                                   
cp: omitting directory `/etc/skel/..'                                  
cp: overwrite `/home/sysadmin/.bash_logout'? n                         
cp: overwrite `/home/sysadmin/.bashrc'? n                              
cp: overwrite `/home/sysadmin/.profile'? n                            
cp: overwrite `/home/sysadmin/.selected_editor'? n                     
sysadmin@localhost:~$
Where the output says ...omitting directory..., the cp command is saying that it cannot copy this item because the command does not copy directories by default. However, the -r option to the cp command will have it copy both files and directories.
Be careful with this option: the entire directory structure will be copied. This could result in copying a lot of files and directories!



Moving Files

To move a file, use the mv command. The syntax for the mv command is much like the cp command:
mv [source] [destination]
In the following example, the hosts file that was generated earlier is moved from the current directory to the Videos directory:
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  example.txt  hosts.copy     
Documents  Music      Public    Videos     hosts                       
sysadmin@localhost:~$ mv hosts Videos                                  
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  example.txt                 
Documents  Music      Public    Videos     hosts.copy                 
sysadmin@localhost:~$ ls Videos                                        
hosts                                                                 
sysadmin@localhost:~$
When a file is moved, the file is removed from the original location and placed in a new location. This can be somewhat tricky in Linux because users need specific permissions to remove files from a directory. If you don't have the right permissions, you will receive a "Permission denied" error message:
sysadmin@localhost:~$ mv /etc/hosts .                                  
mv: cannot move `/etc/hosts' to `./hosts': Permission denied           
sysadmin@localhost:~$
A detailed discussion of permissions is provided in a later chapter.

Moving Files While Renaming

If the destination for the mv command is a directory, the file will be moved to the directory specified. The file name will change only if a destination file name is also specified.
If a destination directory is not specified, the file will be renamed using the destination file name and remain in the source directory.
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  example.txt                 
Documents  Music      Public    Videos                    
sysadmin@localhost:~$ mv example.txt Videos/newexample.txt             
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates                  
Documents  Music      Public    Videos                                 
sysadmin@localhost:~$ ls Videos                                       
hosts  newexample.txt                                                  
sysadmin@localhost:~$

1 Renaming Files

The mv command is not just used to move a file, but also to rename a file. For example, the following commands will rename the newexample.txt file to myexample.txt:
sysadmin@localhost:~$ cd Videos                                        
sysadmin@localhost:~/Videos$ ls                                        
hosts  newexample.txt                                                  
sysadmin@localhost:~/Videos$ mv newexample.txt myexample.txt           
sysadmin@localhost:~/Videos$ ls                                        
hosts  myexample.txt                                                   
sysadmin@localhost:~/Videos$
Think of the previous mv example to mean "move the newexample.txt file from the current directory back into the current directory and give the new file the name myexample.txt”.

2 Additional mv Options

Like the cp command, the mv command provides the following options:
OptionMeaning
-iInteractive move: ask if a file is to be overwritten.
-nDo not overwrite a destination files' contents
-vVerbose: show the resulting move
Important: There is no -r option as the mv command will by default move directories.

Creating Files

There are several ways of creating a new file, including using a program designed to edit a file (a text editor). In a later chapter, text editors will be covered.
There is also a way to simply create a file that can be populated with data at a later time. This is a useful feature since for some operating system features, the very existence of a file could alter how a command or service works. It is also useful to create a file as a "placeholder" to remind you to create the file contents at a later time.
To create an empty file, use the touch command as demonstrated below:
sysadmin@localhost:~$ ls                                                
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  
Videos 
sysadmin@localhost:~$ touch sample                                     
sysadmin@localhost:~$ ls -l sample                                     
-rw-rw-r-- 1 sysadmin sysadmin 0 Nov  9 16:48 sample                   
sysadmin@localhost:~$
Notice the size of the new file is 0 bytes. As previously mentioned, the touchcommand doesn't place any data within the new file.

Removing Files

To delete a file, use the rm command:
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  sample                      
Documents  Music      Public    Videos                                 
sysadmin@localhost:~$ rm sample                                        
sysadmin@localhost:~$ ls                                               
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  
Videos       
sysadmin@localhost:~$
Note that the file was deleted with no questions asked. This could cause problems when deleting multiple files by using glob characters, for example: rm *.txt. Because these files are deleted without question, a user could end up deleting files that were not intended to be deleted.
Additionally, the files are permanently deleted. There is no command to undelete a file and no "trash can" from which to recover deleted files. As a precaution, users should use the -i option when deleting multiple files:
sysadmin@localhost:~$ touch sample.txt example.txt test.txt            
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  example.txt  test.txt       
Documents  Music      Public    Videos     sample.txt                 
sysadmin@localhost:~$ rm -i *.txt                                     
rm: remove regular empty file `example.txt'? y                         
rm: remove regular empty file `sample.txt'? n                          
rm: remove regular empty file `test.txt'? y                            
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  sample.txt                  
Documents  Music      Public    Videos                                 
sysadmin@localhost:~$

Removing Directories

You can delete directories using the rm command. However, the default usage (no options) of the rm command will fail to delete a directory:
sysadmin@localhost:~$ rm Videos                                        
rm: cannot remove `Videos': Is a directory                            
sysadmin@localhost:~$
If you want to delete a directory, use the -r option to the rm command:
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates  sample.txt                  
Documents  Music      Public    Videos                                 
sysadmin@localhost:~$ rm -r Videos                                     
sysadmin@localhost:~$ ls                                               
Desktop  Documents  Downloads  Music  Pictures  Public  Templates 
sample.txt 
sysadmin@localhost:~$
Important: When a user deletes a directory, all of the files and subdirectories are deleted without any interactive question. It is best to use the -i option with the rm command.
You can also delete a directory with the rmdir command, but only if the directory is empty.

Making Directories

To create a directory, use the mkdir command:
sysadmin@localhost:~$ ls                                               
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  
sample.txt
sysadmin@localhost:~$ mkdir test                                       
sysadmin@localhost:~$ ls                                               
Desktop    Downloads  Pictures  Templates   test                       
Documents  Music      Public    sample.txt                             
sysadmin@localhost:~$












































































Load disqus comments

0 comments