-2

I have a file.txt with a list of 100 files and their paths. Here are the first 3 entries:

/project/msun/USERS/me/czi/data/lb/MAP-9-003/c44caf4c-cab7-4749-9940-a74a66bceec3/c44caf4c-cab7-4749-9940-a74a66bceec3.bam
/project/msun/USERS/me/czi/data/lb/MAP-9-007/3e5a10e0-3928-40c5-8dfb-9bbe9d5e0105/3e5a10e0-3928-40c5-8dfb-9bbe9d5e0105.bam
/project/msun/USERS/me/czi/data/lb/MAP-9-013/e230fce2-8f54-4d30-bb75-38d11a438f68/e230fce2-8f54-4d30-bb75-38d11a438f68.bam

I want to cp these files, rename the new files and put them in another dir & path, where the new dir name is the dir in the 8th level of the old path, and the new file name = new dir name + file extension. The new dir location also changes. I would get the new files sorted like this:

/project/msun/USERS/me/czi/data/map/MAP-9-003/MAP-9-003.bam #(old c44caf4c-cab7-4749-9940-a74a66bceec3.bam)
/project/msun/USERS/me/czi/data/map/MAP-9-007/MAP-9-007.bam #(old 3e5a10e0-3928-40c5-8dfb-9bbe9d5e0105.bam)
/project/msun/USERS/me/czi/data/map/MAP-9-013/MAP-9-013.bam #(old e230fce2-8f54-4d30-bb75-38d11a438f68.bam)

I found these posts: Using xargs to copy directories and Copy files with certain extension from many nested sub-directories to a single directory and append to each copied file the name of the directory

doing related tasks but I am unable to do my specific task

Lucas
  • 99
  • Welcome to the site. Please edit your post to indicate what you already tried, and where you faced specific problems. That way you can avoid receiving answers that you alread know won't work, or that rely on tools you don't have installed. – AdminBee Mar 02 '23 at 08:42

1 Answers1

1

I let you copy the files where you want. To rename, using Perl's rename:

Remove -n switch, aka dry-run when your attempts are satisfactory to rename for real.

mkdir -p ./project/msun/USERS/me/czi/data/map
rename -n 's@(project/msun/USERS/me/czi/data/)lb/(MAP-.*?)/.*/.*.bam@$1map/$2.bam@' ./project/msun/USERS/me/czi/data/lb/MAP-9-0*/*/*.bam
# rm -rf ./project/msun/USERS/me/czi/data/lb/MAP*
  • Thank you @Gilles Quénot ! Will the .bam files be renamed in the original folder or in the new folder? I'd like them to be renamed only in the new folder, please. – Lucas Mar 02 '23 at 14:55
  • In the new one. The -n permit to test without doing for real as explained, to renane for real, remove -n switch. With -n you have just the output of what will be renamed in test mode. – Gilles Quénot Mar 02 '23 at 16:22
  • Thank you @Gilles. Apparently rename needs more arguments, with or without -n:
    rename: not enough arguments
    Try 'rename --help' for more information.
    
    – Lucas Mar 02 '23 at 17:17
  • What is the output of rename --version ? – Gilles Quénot Mar 02 '23 at 22:18
  • it is: rename from util-linux 2.32.1 – Lucas Mar 02 '23 at 22:33
  • Which OS do you run? – Gilles Quénot Mar 02 '23 at 22:37
  • I am running it in a remote server:

    $ lsb_release -a LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch Distributor ID: RedHatEnterprise Description: Red Hat Enterprise Linux release 8.4 (Ootpa) Release: 8.4 Codename: Ootpa

    – Lucas Mar 02 '23 at 23:02
  • cpan File::Rename Then search file-rename – Gilles Quénot Mar 03 '23 at 00:28
  • $ cpan File::Rename perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LC_CTYPE = "UTF-8", LANG = "en_US.UTF-8" are supported and installed on your system. perl: warning: Falling back to a fallback locale ("en_US.UTF-8"). Loading internal null logger. Install Log::Log4perl for logging messages Reading '/home/me/.cpan/Metadata' CPAN: Module::CoreList loaded ok (v5.20181130) File::Rename is up to date (2.01).

    $ file-rename bash: file-rename: command not found...

    – Lucas Mar 03 '23 at 00:39
  • find ~ -name 'file-rename*' or dnf install prename – Gilles Quénot Mar 03 '23 at 00:56
  • If I replace "rename -n" by find ~ -name 'file-rename*' or "dnf install prename", then it doesn't work. Maybe I should look for a unix-only solution – Lucas Mar 03 '23 at 14:59