0

I need to add the creation date of the file with the file name. Eg if a creation date of the file Test is 22 Mar 2017, then I need to change it as test_rbc_22032017. I am able to perform for a single file but not sure how to create a script to change the name for n number of files in a given directory. I tried the below but no luck

for f in /path ; do mv -n ""$f"_RBS_(date -r "$f" +"%Y%M%D")"; done

but no luck. please suggest a solution

marked as duplicate by don_crissti, Jeff Schaller, thrig, G-Man, mdpc Mar 24 '17 at 0:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Given unix by default has no creation date, how are you obtaining the creation date of the file? – thrig Mar 22 '17 at 13:47
  • 2
    Command substitution is $(somecmd...), see here and here – ilkkachu Mar 22 '17 at 13:48
  • it's modified date as files that I am looking for is created on everyday basis so both are same . We can take it as modified date and unchanged – user2310119 Mar 22 '17 at 13:50
  • What happened when you tried that command? Please include that information in the question. – Mikel Mar 22 '17 at 15:32
  • i made few changes and used the below 'for f in *; do mv -- "$f" "$f-RBS_$(date +%Y%m%d -r "${f}")"; done' it's working fine – user2310119 Mar 23 '17 at 6:29
1

Your command has a few problems:

for f in /path ; do mv -n ""$f"_RBS_(date -r "$f" +"%Y%M%D")"; done
  • for f in /path will only go once around the loop (with f = /path). You probably want for f in /path/* to consider every file in the /path/ directory (if you want to consider files in subdirectories, that's a whole nother question).

  • mv needs two arguments. The source is obviously "$f". The destination needs to be the transformed name.

  • You missed the $ in the $(...) command substitution.

  • The format string for date doesn't match your description (and you probably want month and day rather than minute and short-date)

  • You're using date of modification, not creation date (which isn't kept, on most filesystem types). I'll leave this as-is.

  • Your description implies that you want the filename to be in lower case.

With the above corrected (apart from the choice of which date to use), we end up with

for f in /path/* ; do mv -n "$f" "${f,,}_rbc_$(date -r "$f" +'%d%m%Y')"; done

Another thing to consider is that if you're doing this more than once in the same place, you might want to move the files to a different directory or test to see if they already have a plausible suffix (either remove it before adding the new one, or just leave such files untouched).

1

First thing is that the mv command takes a source and destination, you are only providing one.

Next thing is the date format, according to your information you want %Y for the year, %m for the month and %d for the day, see man date for an explanation of the differences between the sequences.

Third point is that you only provide the path (for f in /path) instead of referring to files (for f in /path/*). So a working example will be:

for f in /path/*; do mv -n "$f" "${f}_$(date -r "$f" +'%Y%m%d')"; done
  • 1
    @Lambert:  Please quote $f, as in date -r "$f" +…. – G-Man Mar 24 '17 at 2:48
0

try this kind operation, and adapt it to your situation: now=$(date +"%m_%d_%Y") file="/nas/backup_$now.sql"

so should work: for f in /path ; do mv -n "$f" "${f}_RBS_$(date +"%m_%d_%Y")"; done

  • Another query reg the same. I want to find a file in a unix directory and if exist I want to assign the filename along with its modified time stamp to a variable like the below variable=(find the file name, if exist add the filename with _a string _and modified time stamp) is it possible. – user2310119 Mar 22 '17 at 14:22
-3

This is the best way to do it.

Here is the script to add the creation date of the file for n number of files in given directory.

#!/bin/bash

path="/home/modassir/data/"
listoffiles=$(ls -1 /home/modassir/data)


for f in $listoffiles; do
   file=$(date -r $path$f +'%d%m%Y-%H:%M:%S')
   mv $path''$f'' $path''$f''_rbc_''$file''
done;

/home/modassir/data is the directory name

  • 1
    No, using ls is definitely not the best way, see Bash pitfall #1. Also, the back-to-back single-quotes are useless, they just contain an empty string that gets concatenated to the variables (after word-splitting) – ilkkachu Mar 22 '17 at 16:10
  • go to linuxconfig.org/bash-scripting-tutorial and search for "Bash for loop" .If I am wrong,then what is this. To list all files,we can use ls . different website says different format. – Modassir Haider Mar 22 '17 at 16:45
  • Sure, as long as any of the filenames don't contain whitespace, glob characters or unprintable characters (in the opinion of your local ls) and don't start with dashes, it just might work. But you could also use a glob in the the for loop to begin with. – ilkkachu Mar 22 '17 at 17:08
  • 1
    @Modassir - there's a lot of bad advice in that "tutorial". You should try to forget you ever read it, and go somewhere more reliable, such as Greg's Wiki. – Toby Speight Mar 23 '17 at 16:02
  • @Toby ok.buddy I will start reading Greg's Wiki.If I will find it;s better tutorials then will go ahead with this.Otherwise,will follow tutorials point. – Modassir Haider Mar 23 '17 at 17:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.