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

don_crissti
  • 82,805

4 Answers4

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
Lambert
  • 12,680
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).

Toby Speight
  • 8,678
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

ilkkachu
  • 138,973
darvark
  • 285
  • 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 https://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