Linux Shell Commands - Part 7

Hello Friends, Lets see how to copy and delete files in Linux from Shell.




1. Copying

'cp' command is used to copy files from one location to another location.

    a. 'cp' with no arguments will copy from one file to another, silently, in case of an existing file, it will be overwritten.

    root@bt:~/Desktop/Ckorner/post7/exampleDir# ls -l
    total 8
    -rw-r--r-- 1 root root 84 2012-07-15 12:39 newfile1.txt
    -rw-r--r-- 1 root root 84 2012-07-15 12:37 newfile.txt
   
root@bt:~/Desktop/Ckorner/post7/exampleDir# cp newfile.txt newfile1.txt
   
root@bt:~/Desktop/Ckorner/post7/exampleDir#

    Note here, 'newfile1.txt' was already in the directory, since no argument was used, the file was overwritten.

b. 'cp -i file.txt newfile.txt' does the same thing, except in case of file with same name found, it will give a warning message

   
root@bt:~/Desktop/Ckorner/post7/exampleDir# ls -l
    total 8
    -rw-r--r-- 1 root root 84 2012-07-15 12:39 newfile1.txt
    -rw-r--r-- 1 root root 84 2012-07-15 12:37 newfile.txt
   
root@bt:~/Desktop/Ckorner/post7/exampleDir# cp -i newfile.txt newfile1.txt
    cp: overwrite `newfile1.txt'? no
   
root@bt:~/Desktop/Ckorner/post7/exampleDir#

    Note here, an overwrite warning was displayed.

c. 'cp -v' turns on the verbocity, it actually shows the result of the command.

   
root@bt:~/Desktop/Ckorner/post7/exampleDir# cd ..
   
root@bt:~/Desktop/Ckorner/post7# ls
    exampleDir  shellcom7new.txt  shellcom7.txt
   
root@bt:~/Desktop/Ckorner/post7# cp -v shellcom7.txt exampleDir/copiedfile.txt
    `shellcom7.txt' -> `exampleDir/copiedfile.txt'
   
root@bt:~/Desktop/Ckorner/post7#


2. Deleting Files.

a. 'rm' command is used to delete files

   
root@bt:~/Desktop/Ckorner/post7# ls -l
    total 12
    drwxr-xr-x 2 root root 4096 2012-07-15 12:43 exampleDir
    -rw-r--r-- 1 root root   84 2012-07-15 12:46 shellcom7new.txt
    -rw-r--r-- 1 root root   84 2012-07-15 12:35 shellcom7.txt
   
root@bt:~/Desktop/Ckorner/post7# rm shellcom7new.txt
   
root@bt:~/Desktop/Ckorner/post7# ls -l
    total 8
    drwxr-xr-x 2 root root 4096 2012-07-15 12:43 exampleDir
    -rw-r--r-- 1 root root   84 2012-07-15 12:35 shellcom7.txt
   
root@bt:~/Desktop/Ckorner/post7#

   
root@bt:~/Desktop/Ckorner/post7# rm exampleDir/
    rm: cannot remove `exampleDir/': Is a directory
   
root@bt:~/Desktop/Ckorner/post7#

b. 'rm -rf filename/directory' will recursively and forcefully delete files or folder

   
root@bt:~/Desktop/Ckorner/post7# rm -rf exampleDir/
    root@bt:~/Desktop/Ckorner/post7# ls -l
    total 4
    -rw-r--r-- 1 root root 84 2012-07-15 12:35 shellcom7.txt
   
root@bt:~/Desktop/Ckorner/post7#


Tips: Sometimes Its very difficult to understand whether the command has been succesfully completed or not, in that case, we can check by 'echo $?'

root@bt:~/Desktop/Ckorner/post7# echo $?
0

root@bt
:~/Desktop/Ckorner/post7#


'echo $?' returns the exit status of the last issued command,

if the result is 0, means the command was carried out successfully
if the result is 1, means the command completed with some errors
if the result is 2, the command did not complete.

'echo $?' is more meaningful while chaining commands. Which I will show in later posts.

Thats all for now. Stay tuned for more.

Feel Free To Leave A Comment
If Our Article has Helped You, Support Us By Making A Small Contribution, Thank You!


0 comments: