BASH function to rename file to last mondays date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH function to rename file to last mondays date
# 1  
Old 11-21-2014
BASH function to rename file to last mondays date

I'm trying to create a bash function that will allow me to rename a file name emails.txt to last monday's date

Code:
renem ()
{
        d=`date -d last-monday`
        mv ~/emails.txt ~/myemails/$d
}

but i en up wit this error any suggestion

Code:
mv: target `2014' is not a directory


Last edited by Scrutinizer; 11-24-2014 at 12:57 AM.. Reason: CODE tags
# 2  
Old 11-21-2014
The issue is you don't quote your variable.

Code:
renem ()
{
    d=$(date -d last-monday)
    mv ~/emails.txt ~/myemails/"$d"
}

I would suggest something more compact like:

Code:
renem ()
{
    d=$(date "+%Y%m%d" -d last-monday)
    mv ~/emails.txt ~/myemails/$d
}

# 3  
Old 11-21-2014
Which date format do you need?

date -d last-monday will produce something like:
Code:
Mon Nov 17 00:00:00 PST 2014

You don't want to rename ~/emails.txt to Mon Nov 17 00:00:00 PST 2014, don't you?

If you want it that way, then you need to quote the $d variable, e.g.
Code:
mv ~/emails.txt ~/myemails/"$d"

# 4  
Old 11-24-2014
Quote:
Originally Posted by junior-helper
Which date format do you need?

date -d last-monday will produce something like:
Code:
Mon Nov 17 00:00:00 PST 2014

You don't want to rename ~/emails.txt to Mon Nov 17 00:00:00 PST 2014, don't you?

If you want it that way, then you need to quote the $d variable, e.g.
Code:
mv ~/emails.txt ~/myemails/"$d"

yes thats how i want it show

---------- Post updated at 08:13 PM ---------- Previous update was at 07:59 PM ----------

Quote:
Originally Posted by jlliagre
The issue is you don't quote your variable.

Code:
renem ()
{
    d=$(date -d last-monday)
    mv ~/emails.txt ~/myemails/"$d"
}

I would suggest something more compact like:

Code:
renem ()
{
    d=$(date "+%Y%m%d" -d last-monday)
    mv ~/emails.txt ~/myemails/$d
}


this worked thanks alot.

got any good reading to help with learning functions?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash to rename portion of file using match to another

In the portion of bash below I am using rename to match the $id variable to $file and when a match (there will alwsys be one) is found then the $id is removed from each bam and bam.bai in $file and _test is added to thee file name before the extension. Each of the variables is set correctly but... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Bash to copy file 3 times and rename based on another file

In the below bash I am trying to copy the only text file (always only one) in /home/cmccabe/Desktop/list/QC/metrics.txt and rename each of the 3 text files according to /home/cmccabe/Desktop/test/list.txt using lines 3, 4 ,5. This format (that is list.txt) is always 5 lines. Thank you :). ... (12 Replies)
Discussion started by: cmccabe
12 Replies

3. Shell Programming and Scripting

Bash to rename file after second occurence of underscore

I am trying to use bash to remove the text in all filenames after the second _ in specific files that end in .bam or .vcf. However the bash errors looking for the files in the directory. Thank you :). files in directory ... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

5. Shell Programming and Scripting

Rename the Linux log file to the rotation date

Hi all, could any provide me a solution for the below requirement. I have two files namely abc.log.1 and abc.log.2 The above files have time stamp as Dec 08 and Dec 09 I need to rename the files as abc.log.1_20141208 and abc.log.2_20141209 and move to another bkp directory. Thanks in... (2 Replies)
Discussion started by: bhaskar t
2 Replies

6. UNIX for Advanced & Expert Users

Script to rename file that was generated today and which starts with date

hello, can someone please suggest a script to rename a file that was generated today and filename that being generated daily starts with date, its a xml file. here is example. # find . -type f -mtime -1 ./20130529_4995733057260357019.xml # this finename should be renamed to this format.... (6 Replies)
Discussion started by: bobby320
6 Replies

7. Shell Programming and Scripting

If(Condition) Rename a file with (Date+Time) Stamp

Hi! Please see our current script: #!/usr/bin/ksh if (egrep "This string is found in the log" /a01/bpm.log) then mailx -s "Error from log" me@email.com, him@email.com </a01/bpm.log fi To the above existing script, we need to add the following change: 1) After finding the string,... (7 Replies)
Discussion started by: atechcorp
7 Replies

8. Shell Programming and Scripting

Rename File Based on Created Date

I am trying to rename files based on the created/born date of the file. I Have a total of 4000 files that i am trying to do this with and would like it to be log_yyyymmddhh.gz right now the files are maillog.???.gz. Can anyone point me in the right direction of how to get this done via scipt? ... (4 Replies)
Discussion started by: Paulb
4 Replies

9. Shell Programming and Scripting

copy/rename file as date() unix/shell

File.jpg I want to copy and rename this as 2008-12-02.jpg I tried this copy File.jpg date '%y-%m-%d-%H:%M:%S'.jpg This doesnt work.... what do i do? (1 Reply)
Discussion started by: hdogg
1 Replies

10. Shell Programming and Scripting

Rename a file to have Date and Line Count

Hi, I am trying to come with one line command which will rename a file to have Date (MMDDYYYY) and Line Count in the file. Any lead will be appreciated rgds bmk (2 Replies)
Discussion started by: bmkux
2 Replies
Login or Register to Ask a Question