LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2022, 05:51 AM   #1
LQ496873
Member
 
Registered: Jan 2006
Distribution: openSUSE Leap 15.3
Posts: 120

Rep: Reputation: Disabled
Collate all files from multiple folders


I've got about 30 folders each containing a few hundred files. I want copy all of the files in all of the folders into a new folder.

I've been trying to use rsync but I'm not sure it can do it. Internet searches keep resulting in programming scripts and the obvious...
Code:
cp my_file_name.txt my_file2_name.txt
Can it be done with a somewhat simple command?

Thank you for any help
 
Old 03-05-2022, 05:59 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,333
Blog Entries: 3

Rep: Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730Reputation: 3730
More information is needed but you can do that with the perl version of rename from the 'rename' package. Or else find might be needed, too. How do you plan to calculate the new name?
 
Old 03-05-2022, 06:41 AM   #3
LQ496873
Member
 
Registered: Jan 2006
Distribution: openSUSE Leap 15.3
Posts: 120

Original Poster
Rep: Reputation: Disabled
I did consider using the mv command but like with cp it seems you have to specify every file. I'm unfamiliar with perl and rename... and running scripts too! My only programming has been with VSCode and Python.

Essentially, I've used photorec on an old 3GB IDE drive I found in the garage and I want to put all the files into one folder. I'm not bothered about the names of them because photorec has given them all obscure names already.
 
Old 03-05-2022, 08:58 AM   #4
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
I have done similar when I ripped some audio book CDs onto my PC. Each CD was written into its own folder with the same series of file names and I wound up writing a script that looped through and read the file names then renamed them into one folder with a continuous series of names so the names stayed in the order they would be listened to.

It would be very simple to rename them something like folder-filename sequence, since the folders are likely already unique names.
 
Old 03-05-2022, 04:42 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,144

Rep: Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124Reputation: 4124
Quote:
Originally Posted by LQ496873 View Post
I did consider using the mv command but like with cp it seems you have to specify every file.
Nope - but you will have to specify each of the source directories. If files are mv'd in the same filesystem, there is no copying of the data involved, just an update of the inode. "ls -d */" will give you a list of dirs - may or may not need filtering; try it and see. This should work for the move - "mv dir1/* target"

Last edited by syg00; 03-05-2022 at 11:55 PM. Reason: correct ls command
 
Old 03-05-2022, 11:49 PM   #6
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 634

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
Are the filenames unique and able to just be copied into the new directory?

If your photorec is at /data and new directory is /flat

Code:
find /data -type f -exec cp {} /flat \;
If it's on the same drive and you want to ensure you don't overwrite files

Code:
find /data -type f -exec cp --no-clobber --link {} /flat \;
This scans /data for everything with type f (normal file), then executes a cp of that file to /flat

Last edited by Sefyir; 03-05-2022 at 11:51 PM.
 
Old 03-06-2022, 05:02 AM   #7
LQ496873
Member
 
Registered: Jan 2006
Distribution: openSUSE Leap 15.3
Posts: 120

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Sefyir View Post
Are the filenames unique and able to just be copied into the new directory?

If your photorec is at /data and new directory is /flat

Code:
find /data -type f -exec cp {} /flat \;
If it's on the same drive and you want to ensure you don't overwrite files

Code:
find /data -type f -exec cp --no-clobber --link {} /flat \;
This scans /data for everything with type f (normal file), then executes a cp of that file to /flat
Thank you so much for your help guys... this worked perfectly. I've had many bouts of searching for the answer to no avail, despite the question being asked many times on other websites. I hope this thread can help others in the future.
 
Old 03-11-2022, 04:53 AM   #8
LQ496873
Member
 
Registered: Jan 2006
Distribution: openSUSE Leap 15.3
Posts: 120

Original Poster
Rep: Reputation: Disabled
I've decided to go through the same process with all the other drives I have kicking about. I've accumulated about 25 over the years!
Some are pretty big so I've added to the script so that instead of dumping all the files into one folder they are separated by file type... ie /jpg, /png, /rar etc

The downside is that I have to return to the computer, change "filetype=***" and restart the script. I don't mind restarting it for each drive but it's quite repetitive doing it for each file type on each drive!

Is there a way to change what I've done so far so that "filetype=***" is changed for each filetype in /data until everything has been sorted and /data is deleted?
A bit like... "for i in /data, filetype=i"

Code:
filetype=png 
&& original=data 
&& new=flat 
&& mkdir -p /$new/$filetype/ 
&& find /$original -name "*.$filetype" -type f -exec cp -v {} /$new/$filetype \; 
&& sudo find /$original -name "*.$filetype" -type f -exec rm -v {} \; 
&& find /$original -empty -type d -delete
Thank you!

Last edited by LQ496873; 03-11-2022 at 05:01 AM.
 
Old 03-11-2022, 09:46 AM   #9
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
Of course it can handle more than one path and file type.

Put a loop within a loop so that the outer loop selects the file type and path for output and the inner loop does the processing for the files that match.
 
Old 03-12-2022, 02:04 AM   #10
LQ496873
Member
 
Registered: Jan 2006
Distribution: openSUSE Leap 15.3
Posts: 120

Original Poster
Rep: Reputation: Disabled
Thank you. I think I need to learn scripting properly! I can do a bit of Python and thought about coding it all into a GUI but maybe this is my perfect reason to learn Linux better.

I can use
Code:
find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
to get a list of file extensions that photorec salvaged but getting my script to loop it is proving difficult.

Should I be saving my script as a .sh file and running that? At the moment I'm just running a really long command in the terminal... (the last one being)
Code:
filetype=doc && original=/run/media/richard/8b491c4c-3477-4e22-9ecf-b8492d060e49/80gbo && new=/run/media/richard/8b491c4c-3477-4e22-9ecf-b8492d060e49/80gbBlack && mkdir -p $new/$filetype/ && find $original -name "*.$filetype" -type f -exec cp -v {} $new/$filetype \; && sudo find $original -name "*.$filetype" -type f -exec rm -v {} \; && find $original -empty -type d -delete
I feel I've tried a hunderd versions of things like

Code:
for i in {find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u}
do 
filetype=$i && original=/run/media/richard/8b491c4c-3477-4e22-9ecf-b8492d060e49/80gbo && new=/run/media/richard/8b491c4c-3477-4e22-9ecf-b8492d060e49/80gbBlack && mkdir -p $new/$filetype/ && find $original -name "*.$filetype" -type f -exec cp -v {} $new/$filetype \; && sudo find $original -name "*.$filetype" -type f -exec rm -v {} \; && find $original -empty -type d -delete
That type of thing. Obvioulsy stright away list=... doesn't work.

Last edited by LQ496873; 03-12-2022 at 02:14 AM.
 
Old 03-12-2022, 06:44 AM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,809

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
The following hooks an embedded bash script into a find.
Code:
cd "$original"
find . -type f -exec /bin/bash -c '
  for f
  do
    if [[ $f == *.?* ]]
    then extd=${f##*.}
    else extd=noext
    fi
    mkdir -p "$new/$extd" &&
    cp "$f" "$new/$extd/$f" &&
    sudo rm "$f"
  done
' move.sh {} +

Last edited by MadeInGermany; 03-12-2022 at 06:46 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script to compare folders,Sub-Folders and Sub-Sub-Folders unix_72427 Programming 8 08-08-2012 02:51 PM
copying multiple files in multiple folders ?? stratotak Linux - Newbie 2 02-21-2009 09:41 AM
change permissions of all folders and sub folders cad Linux - General 3 01-28-2007 10:21 AM
ld-collate.o Error in glibc compilation newcomer4 Linux From Scratch 2 02-23-2006 12:27 PM
Collate documents into 1 pdf document: pdffactory equivalent smiley_lauf Linux - Software 5 01-17-2006 12:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration