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,660 points
825 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,270 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
• 26,310 points

edited Jul 2, 2019 by Gitika 121 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
8,753 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, 2019 in Linux Administration by Omkar
• 69,000 points
65 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,280 points
173 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,380 points
621 views
0 votes
1 answer

copy and create destination directory using bash

mkdir -p "$d" && cp file "$d" try ...READ MORE

answered Jul 15, 2019 in Linux Administration by Upasana
• 8,590 points
66 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 aryya
• 7,350 points
154 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,810 points
250 views
0 votes
1 answer