c noak
c noak

Reputation: 31

Move and rename files based on subfolders

I would appreciate any help, relatively new here

I have the following directory structure

Main_dir
|-Barcode_subdirname_01\(many further subfolders)\filename.pdf
|-Barcode_subdirname_02\(many further subfolders)\filename.csv

I've been attempting to write a bash script to do this from the main_dir but have hit the limit of my coding ability (i'm open to any other way that'll work).

Upvotes: 3

Views: 243

Answers (1)

karakfa
karakfa

Reputation: 67467

You can use rename with sed like substitute conventions, for example

$ rename 's~([^_]+)_([^_]+)_.*/([^/.]+\..*)~$1_$2_$3~' barcode_subdir_01/a/b/c/file2.csv

will rename file to

barcode_subdir_file2.csv

I used ~ instead of the more common / separator to make it more clear.

You can test the script with -n option to show the renamed files without actually doing the action.

Upvotes: 1

Related Questions