LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-14-2024, 01:42 PM   #1
mattdawolf
Member
 
Registered: Aug 2018
Posts: 35

Rep: Reputation: Disabled
recursively copying files into single dir broken down by alphabet


How would I get find src/ -type f -name "*.so" -exec cp {} dst/ \;
to do the following:

copy all files in source and relative dirs to destiantion without relative paths, while renaming any potential duplicates to avoid data loss.

destination dir should be broken down into dirs with first letter.

There are 340000 files I need to go through.

Many thanks!
 
Old 05-15-2024, 04:15 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Provided the one-letter-named directories already exist and every affected filename starts with a letter used to name those, I'd try something like this (it won't rename duplicates though)
Code:
find src/ -type f -name \*.so -execdir rename -n -v 's|^./(.)|dst/$1/$1|' {} \;
-n is there for testing it. Remove it to do the actual renaming.

This is Perl-based rename, it may be alternatively called prename or file-rename in your distribution. Don't confuse it with less capable rename from util-linux.

As it allows arbitrary Perl code in its expressions, you probably can adapt it to handle duplicate renaming as well.

Last edited by shruggy; 05-15-2024 at 04:24 AM.
 
Old 05-15-2024, 04:27 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,152

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Quote:
Originally Posted by shruggy View Post
Provided the one-letter-named directories already exist ...
Yes, I was also thinking this would make things much easier.
 
Old 05-15-2024, 05:44 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Well, for anything more fancy, I'd probably write a shell/Perl script and call it from the -exec/-execdir action of find giving it the file name as parameter. The renaming and all the checks would then be done inside the script.
 
Old 05-15-2024, 07:55 AM   #5
jmccue
Member
 
Registered: Nov 2008
Location: US
Distribution: slackware
Posts: 709
Blog Entries: 1

Rep: Reputation: 384Reputation: 384Reputation: 384Reputation: 384
Quote:
Originally Posted by shruggy View Post
Well, for anything more fancy, I'd probably write a shell/Perl script and call it from the -exec/-execdir action of find giving it the file name as parameter. The renaming and all the checks would then be done inside the script.
I would follow this too, plus remember, you may run into duplicate file names, so you may need to account for that too.
 
Old 05-15-2024, 11:26 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,832

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
find with 'embedded' bash code:
Code:
find src/ -type f -name "*.so" -exec /bin/bash -c '
  do=echo # for testing/dry-run
  destdir=${1%/}; shift
  for fn
  do
    basefn=${fn##*/}
    firstchar=${basefn:0:1}
    $do mkdir -p "$destdir/$firstchar"
    dupext= num=0
    while
      destfn="$destdir/$firstchar/$basefn$dupext"
      [ -e "$destfn" ]
    do
      dupext="($((++num)))"
    done   
    $do cp -v "$fn" "$destfn"
  done
' acp.bash dest/ {} +
 
1 members found this post helpful.
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copy files only in dir 2 that are not also in dir 1 into a new dir jr_bob_dobbs Linux - General 6 03-22-2018 10:35 AM
copying files from home dir to another dir from another dir in a lower dir chomito44 Linux - General 5 10-19-2013 06:18 PM
Move files contained in source dir to destination dir, but not source dir itself unixunderground Linux - Software 3 09-20-2013 11:17 AM
tar dir and sub dir removing files but not existing not empty dir j-me Linux - General 2 08-12-2013 11:37 AM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:28 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