Backing up specific files and renaming them via Bash

+1 vote

I want to create backups of certain files which are in /home directory. 

find /root/ -name 'wallet.dat' -exec cp {} /home/backup \;

I also want to change the name of the backup file to a particular format. 

The files are always in a structure like this: /home/<coinname>/.<coinname>/wallet.dat. And I want the backup file to be named <coinname>-wallet.dat so the folder /home/backup has following files:

  • bitcoin-wallet.dat
  • dogecoin-wallet.dat

How can I do this?

Aug 30, 2018 in Linux Administration by digger
• 27,620 points
751 views

3 answers to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

I have mentioned a slightly modified code that you have mentioned in the question. 

find /root/ -name 'wallet.dat' -execdir bash -c 'echo cp "$0" "/home/backup/${PWD##*/}-${0#\./}"' {} \;
answered Aug 30, 2018 by slayer
• 29,040 points
0 votes

Try this

#!/bin/bash
find . -name wallet.dat | while read x
do
   original="$x"
   # Strip filename off right end
   x=${x%/wallet.dat}
   # Strip all but last directory off left end
   x=${x##*/}
   newname="/home/backup/${x}-wallet.dat"
   echo cp "$original" "$newname"
done
answered Aug 30, 2018 by Selvaraj
0 votes

I needed to do a similar thing and I found out this code that uses tar to do it. Code mentioned below:

$ find /root -name wallet.dat -print | tar -cv -T - -f /home/backup/wallets.tar

answered Apr 26 by Gagan

Why use the tar command here?

Hi @Raj, this is a way of creating a copy of files. So, basically, this command will find the required files and send them into a tarfile. This tar file is your backup file.
If anybody is using this command, make sure you run it only when the files are not corrupted. If you run it, irrespective of whether the files are corrupted or not, the new backup will overwrite the old backup.
True that @Tina. To avoid this, I save each backup file with a different name and I mention the date of Backup so that I know which file to use to recover.
@Raghu, this actually gives a checkpoint backup feature.

Related Questions In Linux Administration

0 votes
2 answers

How do I find all files containing specific text on Linux?

grep -rnw 'text to find recursively inside ...READ MORE

answered May 20 in Linux Administration by Ritesh Singh
2,228 views
0 votes
1 answer

How to change permissions of folder and files in folder?

The chmod command is used to set/change the permissions ...READ MORE

answered Jan 31 in Linux Administration by Omkar
• 67,000 points
18 views
0 votes
1 answer

redirecting stdout and stderr to file using bash

cmd >>file.txt 2>&1 Bash always executes and redirects ...READ MORE

answered Mar 19 in Linux Administration by ajs3033
• 7,120 points
21 views
0 votes
1 answer
0 votes
1 answer

Regarding Edquota soft and hard limits

The provided value for soft and hard ...READ MORE

answered Jan 3 in Linux Administration by Omkar
• 67,000 points
20 views
0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31 in Linux Administration by Omkar
• 67,000 points
21 views
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,320 points
50 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
88 views
0 votes
1 answer

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.