I am backing up files including directories from 1 partition to other using cpio. cpio works perfect for backing but I am unable to backup directory tree. I am in a partition mounted on the directory as shown below. In that partition, I am in the directory /b/c. It contains 3 files 'd, e & f'. Also it contains a directory named g which in turn includes files 'h & j'. Please see below.

ravbholua@ravbholua-Aspire-5315:/media/ravbholua/c09fb098-0402-418a-b1e5-8888848a5636/b/c$ ls *
d  e  f

h  j

Now I want to back up all these files (d, e, f, h & j) including directory structure on to a different partition. I have created file (or archive in this case) named 'mark'. Please see below. Using cpio I am transferring to archive named 'mark'.

ravbholua@ravbholua-Aspire-5315:/media/ravbholua/c09fb098-0402-418a-b1e5-8888848a5636/b/c$ ls -R /med*/ravb*/*36/b/c/* | cpio -ov > /med*/ravb*/*9f/mark
cpio: blank line ignored
cpio: /media/ravbholua/c09fb098-0402-418a-b1e5-8888848a5636/b/c/g:: Cannot stat: No such file or directory
cpio: h: Cannot stat: No such file or directory
cpio: j: Cannot stat: No such file or directory
1 block

cpio perfectly transferred the immediate files (d, e & f) in the directory. Problem comes for the directory g and the subdirectory files. It's apparent that cpio requires regular files and doesn't entertain directory. So, now so that the tool cpio includes the directory tree, anyhow I have to give cpio the subdirectory files 'h & j' under directory 'g'. How to do that. ls, ls -R, etc. can't provide only the files in their output, they include directory also.

So, please tell me is there any option to ls or any other command that will output only the files in the directory tree. Have a look below:

$ (command that will output the below)



I'm editing my Q with some correction so that any viewer of this thread would get clear and right information. I mentioned that cpio doesn't accept directories based on what I felt from the output of ls command. But its wrong. cpio accepts directories even. So, cpio to me seems to be a very great tool for backing up selected files or complete partition/disk.

3 Answers 3


You should not process the output from ls into cpio. The 'standard' way of getting file names into cpio is using find:

find /med*/ravb*/*36/b/c/* -type f | cpio ...

or on Linux systems:

find /med*/ravb*/*36/b/c/* -type f -print0 | cpio -0 ...

that handles file names with newlines and other special characters as well. -type f only selects files.

  • yes it has worked fantastically and -d option helped recovery. Thank you. cpio seems to me to be a great tool for backing up data, whether its some specific files & directories or the complete partition/disk. What do you say?
    – Ravi
    Dec 12, 2013 at 12:35
  • If I create a hidden file (ex.: .usa) under directory c, its not included in the back-up but if the hidden file is anywhere else in the directory tree (ex.: .../b/c/g), its getting included. As * ignores the hidden file, its not included under c directory. So, its better to remove *. The numeral 0 suffixed to print and as an option to cpio: has it any significance because I'm seeing 0 like this 1st time.
    – Ravi
    Dec 12, 2013 at 13:01

You can find a good resource here: http://docs.oracle.com/cd/E23824_01/html/821-1459/bkupsavefiles-21.html

-d Creates directories as needed.

  • seems to be a very good link. I will go through it. Thank you
    – Ravi
    Dec 12, 2013 at 12:14
  • Thanks once again for providing the -d option. I backed up the files using the command as mentioned by Zelda. Then I deleted the original only to test the recovery. Now -d came for my rescue.
    – Ravi
    Dec 12, 2013 at 12:33

One of my simple examples:

for dir in it work; do  
    cd /home/$USER/$dir/  
    for file_ext in csv docx pdf txt xlsx; do  
        find . -depth -iname "*.$file_ext" -print0 | cpio --null -dmpv /home/$USER/Dropbox/$dir/  
  • Welcome to the site, and thank you for your contribution. Please note that your approach is very similar to this answer by @Zelda, so you may want to point out the differences in your respective approaches to the problem.
    – AdminBee
    Apr 30, 2020 at 11:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .