How to make a archive using cpio command?

 


The cpio command is a very useful command when it comes to archiving. It works the same as tar command, but it is way better than tar command. In this short article, check out how you can make a archive using cpio command. 


What is cpio command?


GNU/cpio command is a tool for creating and extracting archives, or copying files from one place to another. It handles a number of cpio formats as well as reading and writing tar files. The cpio command has three mode that we need to understand in order to properly use this command. They're as follow:


  • copy-out mode - which allow you to make a archive
  • copy-in mode - extract a archive
  • copy-pass mode - copy files into a destination directory

Here are some very useful samples of using the cpio command. 


1. Create an cpio archive.


In order to make an cpio archive, you can combine the ls command with cpio command. Here's the command.


# ls |  cpio  -ov  >  sample.cpio 




Note that you must be root in order to make a archive using cpio. In the figure above, I have used list command to make a archive of kings directory. The archive name is sample.cpio. Check the figure down bellow.




There is our sample.cpio archive. Once again, the short explanation for the command is as follow.


# ls  |  cpio -ov  > sample.cpio 


  •  o - make an archive
  • v - verbose - show me the files which are being archived 
  • > redirection sing which tells the command to redirect the files into a sample.cpio instead of standard output. 

2. Show the content of the archive


If you don't want to extract the archive, you can show it's content by executing the following command. 


# cpio -it  -F  sample.cpio 




Here's the content of our sample.cpio archive. Now let me to show you how you can extract it.


3. Extract the archive


Currently I'm inside the kings directory within my home directory. If I try to extract this archive with cpio command, the command will fail because these files already exists in the archive and therefore they can't be extracted. But, in case you wanna extract the archive by replacing the original files in directory, you can use the -u switch. Check the following command.


# cpio  -iv  <  sample.cpio 


This -u switch will replace all the files within the kings directory without prompting or asking the user: ' Do you wanna replace the files?. 






There you go. The files have been successfully extracted.


4. Copy all the files to another directory


In order to copy all the files from a kings directory to a newly formed sample_dir directory within my /home, we must use -p flat. Here's the command.

# find . | cpio -pv  /home/amar/sample_dir 




As you can see on figure above, I have used the find command pass all the files into a sample_dir directory. 



This is it; all the files from kings directory have been successfully copied into sample_dir directory. 

 Now you now how to make an archive using cpio command, how to view it's content, how to extract it and copy the content?. Make sure to view others link on cpio command as well. If you like this article, leave a comment down bellow. Till the next time. 


Check these link on cpio command


[1] cpio command in Linux with samples

[2] Linux cpio command samples

Share:

Nema komentara:

Objavi komentar

Zajednica

POPULARNI POSTOVI

Oznake

NAJNOVIJE