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
• 26,740 points
2,442 views

3 answers to this question.

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,370 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, 2019 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 and rename in unix/linux

Hi, @Kal You can use this command below to ...READ MORE

answered Jul 2, 2019 in Linux Administration by Gitika
• 65,770 points

edited Jul 2, 2019 by Gitika 2,724 views
0 votes
0 answers

Renaming multiples files with a bash loop

I need to rename 45 files, and ...READ MORE

Jun 21, 2022 in Linux Administration by Korak
• 5,820 points
387 views
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, 2019 in Linux Administration by Ritesh Singh
40,400 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 Feb 1, 2019 in Linux Administration by Omkar
• 69,220 points
1,988 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, 2019 in Linux Administration by ajs3033
• 7,300 points
22,308 views
0 votes
1 answer

How to extract file base name without path and extension in bash?

You don't actually have to call the ...READ MORE

answered May 31, 2019 in Linux Administration by Shubham
• 13,490 points
5,917 views
+1 vote
1 answer

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

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,427 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
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP