how to rename files while copying?

How would I copy (archive style where date isn’t changed) all the files in a backup directory to the user’s directory while renaming each file to remove the random string portion from the name (i.e., -2b0fd460_1426b77b1ee_-7b8e)?

cp from:

/backup/path/data/Erp.2014.02.16_16.57.03-2b0fd460_1426b77b1ee_-7b8e.etf

to:

/home/user/data/Erp.2014.02.16_16.57.03.etf

Each file will always start with “Erp.” followed by the date-time stamp string followed by the random string and then the extension “.etf”. I want to keep all name elements including the date-time stamp. I just want to remove the random string.

The random string allows multiple backups of the same file. However, in this case, I just ran fdupes and there are no duplicates. So I can simply restore all the files, removing the random string.

I’m looking for a one-line bash command to do it.

If that won’t work, I could do it in two or more steps. I normally use KRename, but in this case I need to do it in bash. (I’m working remotely.)

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can use the cp command with the -a option

-a, --archive
       same as -dR --preserve=all

And then use a for loop to rename all files while copying them:

for file in Erp*etf; do
  cp -a $file destinationDirectory/${file%%-*}.etf
done

Ready. Start this command in the source directory.

Explanation: The %%-* will cut off all the characters after the first occurence of a hyphen/minus - and the .etf at the end again adds the file extension.

Well, and as a one liner, put it all in one line. 🙂 Like this

for file in Erp*etf; do cp -a $file destinationDirectory/${file%%-*}.etf; done

Method 2

pax can do this all at once. You could do:

cd /backup/path/data && pax -wrs'/-.*$/.etf/' Erp*etf /home/user/data

pax preserves times by default, but can add -pe to preserve everything (best done as root) or -pp to preserve permissions , eg:

cd /backup/path/data && pax -wrs'/-.*$/.etf/' -pe Erp*etf /home/user/data

Otherwise (pax isn’t usually available by default), surely it is better to do a copy then a rename:

cp -a /backup/path/data/Erp*.etf /home/user/data
rename 's/-.*$/.etf/' /home/user/data/Erp*.etf

This way there is not a different process started for each file.

Method 3

In zsh, use zmv. Put this in your .zshrc:

autoload -U zmv
alias zcp='zmv -C'
alias zln='zmv -L'

Then use

zcp '/backup/path/data/(*)-[0-9A-Fa-f_]#.(*)' '/home/user/data/$1$2'

In bash:

zsh -c 'autoload zmv; zmv -C $0 $1' '/backup/path/data/(*)-[0-9A-Fa-f_]#.(*)' '/home/user/data/$1$2'

If you don’t have zsh, a POSIX way uses pax (this copies directories recursively).

If you’re on a restricted system with no zsh and no pax, you can use a loop:

for source in /backup/path/data/*-*.etf; do
  basename=${source##*/}
  cp "$source" "/home/user/data/${basename%-*}.${basename##*.}"
done

Method 4

cp doesn’t have that capability. I recall cpio being able to do that, but the current manpage says otherwise. However, (gnu) tar does have a --transform option:

 --transform, --xform EXPRESSION
       use sed replace EXPRESSION to transform file names

So you’d have a cmdline like:

(cd /backup/path/data; tar --create --transform 's/-.*-....//' .) | (cd /home/user/data; tar --extract)


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x