Views:
10,393β
Votes: 4β
β
Solution
Tags:
command-line
cp
ms-dos
Link:
π See Original Answer on Ask Ubuntu β§ π
URL:
https://askubuntu.com/q/1153995
Title:
Copy group of files (Filename*) to backup (Filename*.bak)
ID:
/2019/06/26/Copy-group-of-files-_Filename*_-to-backup-_Filename*.bak_
Created:
June 26, 2019
Edited: June 27, 2019
Upload:
September 15, 2024
Layout: post
TOC:
false
Navigation: false
Copy to clipboard: false
I wrote this one-liner into my ~/.bashrc
. Much better answers using find
can be posted I suppose. Even better answers could be written in in C. Hopefully this Q&A gets the ball rolling for better answers:
cps () {
# cps "Copy Splat", copy group of files to backup, ie "cps Filename .bak"
# Copies Filename1 to Filename1.bak, Filename2 to Filename2.bak, etc.
# If Filename1.bak exists, don't copy it to Filename1.bak.bak
for f in "$1"*; do [[ ! "$f" == *"$2" ]] && cp -a "$f" "$f$2"; done
# OLD version comments suggested to remove
# ls "$1"* | while read varname; do cp -a "$varname" "$varname$2"; done
}
for f in "$1"*; do
:$1
is thegmail-meta3
parameter andf
is the list of files matching. Combined this means for gmail-meta3, gmail-meta3-LAB-9999, etc. do the following[[ ! "$f" == *"$2" ]] &&
:$f
is the same asf
above.$2
is the.bak
parameter passed. Combined this means if the filename doesnβt end in.bak
(because we donβt want to copy.bak
and create.bak.bak
) then do the followingcp -a "$f" "$f$2";
copy gmail-meta3 to gmail-meta3.bak, etc.done
: loop back and grab next filename ingmail-meta3
* list.
cps gmail-meta3 .bak
Sample Output
Using the question as an example here is how it looks in action:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@alien:~/gmail$ ll gmail-meta3*
-rw-rw-r-- 1 rick rick 26467821 May 20 16:43 gmail-meta3
-rw-rw-r-- 1 rick rick 26467821 May 20 16:43 gmail-meta3.bak
-rw-rw-r-- 1 rick rick 643 May 20 16:43 gmail-meta3-LAB-1558392194-26467821
-rw-rw-r-- 1 rick rick 643 May 20 16:43 gmail-meta3-LAB-1558392194-26467821.bak
-rw-rw-r-- 1 rick rick 49607 May 20 16:44 gmail-meta3-REC-1558392194-26467821
-rw-rw-r-- 1 rick rick 49607 May 20 16:44 gmail-meta3-REC-1558392194-26467821.bak
-rw-rw-r-- 1 rick rick 728954 Jun 27 17:04 gmail-meta3-YAD-1558392194-26467821
-rw-rw-r-- 1 rick rick 728954 Jun 27 05:46 gmail-meta3-YAD-1558392194-26467821.bak
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@alien:~/gmail$ cps gmail-meta3 .bak
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@alien:~/gmail$ ll gmail-meta3*
-rw-rw-r-- 1 rick rick 26467821 May 20 16:43 gmail-meta3
-rw-rw-r-- 1 rick rick 26467821 May 20 16:43 gmail-meta3.bak
-rw-rw-r-- 1 rick rick 643 May 20 16:43 gmail-meta3-LAB-1558392194-26467821
-rw-rw-r-- 1 rick rick 643 May 20 16:43 gmail-meta3-LAB-1558392194-26467821.bak
-rw-rw-r-- 1 rick rick 49607 May 20 16:44 gmail-meta3-REC-1558392194-26467821
-rw-rw-r-- 1 rick rick 49607 May 20 16:44 gmail-meta3-REC-1558392194-26467821.bak
-rw-rw-r-- 1 rick rick 728954 Jun 27 17:04 gmail-meta3-YAD-1558392194-26467821
-rw-rw-r-- 1 rick rick 728954 Jun 27 17:04 gmail-meta3-YAD-1558392194-26467821.bak
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
rick@alien:~/gmail$
Note: This uses the -a
flag with the cp
command to preserve timestamps and give you better grasp of your file backups.
Notice how the file copies have the exact same date and time as the originals. If the -a
parameter was omitted they would be given the current date and time and wouldnβt look like a true backup except that the file size would be the same.