LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2022, 12:43 AM   #1
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Rep: Reputation: Disabled
copy with appended timestamp


I am trying to create a .sh file that will copy a file and append the timestamp right before the extension.

I am trying the following.

#!/bin/bash
cp -p /home/oracle/Desktop/folder_1/movefile.txt /home/oracle/Desktop/folder_2/movefile{,"-$(date +"%Y%m%d%H%M%S")"}.txt

the error I am seeing when I run from the terminal is

bash archive.sh
cp: target ‘/home/oracle/Desktop/folder_2/movefile-20220831014047.txt’ is not a directory


any help or pointers will be appreciated.
 
Old 08-31-2022, 02:32 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
use set -xv before executing this command and you will see how is it interpreted by the shell.
And you will see your command is expanded into 5 parts (something like this):
Code:
cp                                                        # the command itself
-p                                                        # a flag
/home/oracle/Desktop/folder_1/movefile.txt                # first argument
/home/oracle/Desktop/folder_2/movefile.txt                # second argument
/home/oracle/Desktop/folder_2/movefile-20220831014047.txt # third
 
1 members found this post helpful.
Old 08-31-2022, 05:21 AM   #3
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
remove the curly braces eg
Code:
cp -p a/b/c/xxx.txt "a/b/xxx-$(date +"%Y%m%d%H%M%S").txt"
keithhedger@LFSInitDev [ /tmp ]$ ls a/b
c  xxx-20220831111919.txt
 
1 members found this post helpful.
Old 08-31-2022, 08:45 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
To elaborate on the previous posts.
Code:
/path/movefile{,"-$(date +"%Y%m%d%H%M%S")"}.txt
The {} is called brace expansion. As an example a{b,c}d will expand to
abd and acd

Or in your case a{,c}d expands to
ad and acd
i.e.
/home/oracle/Desktop/folder_2/movefile.txt
and
/home/oracle/Desktop/folder_2/movefile-20220831014047.txt

which does not produce the intended results. In your case brace expansion is not necessary and you can just combine a command substitution i.e. $() together in a fixed string normally without needing anything else as in post #3.
 
1 members found this post helpful.
Old 09-04-2022, 12:01 AM   #5
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Original Poster
Rep: Reputation: Disabled
pan64 Thanks for the tip that will help me see what is going on.
Keith Hedger That's the fix thank you.
michaelk Thanks for the clarification.


Thank you .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
script to timestamp files with timestamp from directory eRJe Programming 4 11-13-2013 06:52 PM
Rename session_id with appended user name jilljack Programming 2 10-12-2005 12:30 PM
PATH appended with . ? kaon Slackware 1 04-19-2005 09:48 AM
Wierd String appended to end of line lel800 General 8 12-28-2004 03:55 PM
howto get rid of files with ~ appended random234 Linux - Software 3 06-16-2004 09:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration