Cpio all *.txt-files out of folders to just one directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cpio all *.txt-files out of folders to just one directory
# 1  
Old 08-09-2015
Cpio all *.txt-files out of folders to just one directory

I need a hint for reading manpage (I did rtfm really) of cpio to do this task as in the headline described. I want to put all files of a certain type, lets say all *.txt files or any other format. Spread in more than hundreds of subdirectories in one directory I would like to select them and just move, copy them all in one directory. As using the following comand it gets me some trouble, may someone could give me a hint.
Code:
find /path/to/folder .*.txt -depth -print0 | cpio --null -pvd ../new/folder

Thanks in advance!!!
# 2  
Old 08-09-2015
The issue with using cpio is that it preserves the directory hierarchy. You could go with:
Code:
find /.../sourcedir -type f -name '*.txt' -exec cp {} /.../targetdir

but that would generate a process for every file, and cp is not most efficient way to copy files.

Are you trying to just flatten the directory hierarchy? If so, perhaps something like:
Code:
find /.../sourcedir -type f -name '*.txt' -print0 | xargs -0 ln -t /.../targetdir

which would create hardlinks to all the files in the target directory. Use the -s option of ln if the target directory is in a different filesystem.

If you need to make a copy of the files:
Code:
TMPDIR=$(mktemp -d)
trap "rm -f ${TMPDIR}" EXIT
find /.../sourcedir -type f -name '*.txt' -print0 | xargs -0 ln -s -t ${TMPDIR}
cd ${TMPDIR}
ls -1fA | cpio -pLvum /.../targetdir

Be advised that nothing is being done for filename collisions.

You also specified find /path/to/folder .*.txt. The -name option is missing, and use single-quotes. Otherwise the shell globbing expansion may give you a surprise.
This User Gave Thanks to derekludwig For This Post:
# 3  
Old 08-10-2015
solved Cpio all *.txt-files out of folders to just one directory

Thats helping, I see also one mistake of mine, as you mentioned the -name flag and single quotes, therefore I tried instead -iname without any quotes.

But this is rrreaally helpful, yes to flaten the structure. Thanks a lot.
# 4  
Old 08-10-2015
No. Always use the quotes for every -name or -iname primary operand. (You can get by without it under certain circumstances and with the lack of files matching a pattern in your current working directory; but when you try the same script later after some files have been added or when you run it in a different directory, your script with stop working. Even when the quotes aren't required, adding them will never cause you a problem.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can i add each line from a txt file to different files in the same directory?

Hello, this is my first thread here :) So i have a text file that contains words in each line like abcd efgh ijkl mnop and i have 4 txt files, i want to add each line to each file, like file 1 gets abcd at the end; file 2 gets efgh at the end .... I tried with: cat test | while read -r... (6 Replies)
Discussion started by: azaiiez
6 Replies

2. UNIX for Advanced & Expert Users

Cpio - input files (from list) are stored in different order inside cpio archive - why?

Due to budget constraints I have to reinvent an Enterprise backup system in a SPARC (sun4v) Solaris estate (10 & 11). (yep - reinvent wheel, fun but time consuming. Is this wise?! :confused: ) For each filesystem of interest, to try to capture a 'catalog' at the front of each cpio archive (for... (1 Reply)
Discussion started by: am115998
1 Replies

3. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

4. Shell Programming and Scripting

Save files in directory as txt

wget -x -i link.txt The above downloads and create unique entries for the 97 links in the text file. However, each new file is saved as CM080 with a FILE extention. Is there a way to convert each file in that directory to a .txt? The 97 files are in... (12 Replies)
Discussion started by: cmccabe
12 Replies

5. Shell Programming and Scripting

Copy the files in directory and sub folders as it is to another directory.

How to copy files from one directory to another directory with the subfolders copied. If i have folder1/sub1/sub2/* it needs to copy files to folder2/sub1/sub2/*. I do not want to create sub folders in folder2. Can copy command create them automatically? I tried cp -a and cp -R but did... (4 Replies)
Discussion started by: santosh2626
4 Replies

6. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

7. Shell Programming and Scripting

moving the files in a.txt files to a different directory

HI All, I am coding a shell script which will pick all the .csv files in a particular directoryand write it in to a .txt file, this .txt file i will use as a source in datastage for processing. now after the processing is done I have to move and archive all the files in the .txt file to a... (5 Replies)
Discussion started by: subhasri_2020
5 Replies

8. Shell Programming and Scripting

Checking if the files in a directory have a txt extension

foreach file ($dir1/*) if ($file ~ *.txt) then echo "Skipping $file (is a txt file)" endif end that should work right guys? :confused: (15 Replies)
Discussion started by: pantelis
15 Replies

9. UNIX for Advanced & Expert Users

mv OR cp many files from 1 big directory with sub folders all to 1 spot?

Hello All. I am trying to do this from a terminal prompt on my mac.... I have 100 folders all named different things. Those 100 folders are inside ~/Desktop/Pictures directory. Each of the 100 folders are uniquely named. The image files inside of each folder only have some similarities. ... (1 Reply)
Discussion started by: yoyoyo777
1 Replies

10. UNIX for Dummies Questions & Answers

List all files except *.txt in a directory

I have many types of files (Eg: *.log, *.rpt, *.txt, *.dat) in a directory. I want to display all file types except *.txt. What is the command to display all files except "*.txt" (9 Replies)
Discussion started by: apsprabhu
9 Replies
Login or Register to Ask a Question