0

I'm trying to get an example working:

Here is what I'm trying to do:

a) There are 7 files in a folder with the name and timestamp appended.

Examples : Windows_<timestamp>.csv and Linux_<timestamp>.csv so on and so forth.

I want to first a) Move the files that I'm about to re-name to a new folder as is and then rename the current file.

I've tried looking at Rename multiple files by replacing a particular pattern in the filenames using a shell script but that script isn't working for me. I believe I have to modify something in there, but I cant seem to get it work.

Can anyone please help me? I'm really stuck here.

Thanks!

Community
  • 1
  • 1
abhididdigi
  • 361
  • 4
  • 16
  • For this to work, I will have to first iterate over all the files in the directory. Here is the script I'm using to do that `#!/bin/bash FILES=/home/abhididdigi/Desktop/TADDM/* for f in $FILES do echo "Processing $f file..." # take action on each file. $f store current file name cat $f done` I'm stuck here. this doesn't give me a filename as output, this gives me the entire folder path. Once I get this working, the plan is to inject what ever the code is in the above link and get it to work. – abhididdigi Nov 03 '14 at 15:04
  • change the `for` as `for f in home/abhididdigi/Desktop/TADDM/* do` – nu11p01n73R Nov 03 '14 at 15:12
  • I tried that, it gives me 'do' isn't a file. #!/bin/bash for f in home/abhididdigi/Desktop/TADDM/* do do echo "Processing $f file..." # take action on each file. $f store current file name cat $f done and I get : cat: home/abhididdigi/Desktop/TADDM/*: No such file or directory – abhididdigi Nov 03 '14 at 15:21
  • No. i excpected you to complete the `for` as `for f in home/abhididdigi/Desktop/TADDM/* do echo "Processing $f file..." done` – nu11p01n73R Nov 03 '14 at 15:22
  • The relation is, I just need to strip everything after the underscore ( _ ) including the underscore. – abhididdigi Nov 03 '14 at 15:23
  • you missed a `/` infront of `home` it should be `/home/abhididdigi/Desktop/TADDM/*` – nu11p01n73R Nov 03 '14 at 15:27
  • Thank you that works. I'll try to stick in the other part and get it to work. – abhididdigi Nov 03 '14 at 15:28
  • do you want me to add an answer or work on your own?? – nu11p01n73R Nov 03 '14 at 15:30

1 Answers1

1
#!/bin/bash
# find all the files that are created today and end with an extension.
# a) First find all the files created today, and filter only the files we care about.
# b) Move all these files into a new folder.
# c) Iterate all the files in this new folder.
# d) Re-name all the files in the destination folder by replacing the _
sourceFolderName="/home/abhididdigi/Desktop/TADDM"
targetFolderName="/home/abhididdigi/Desktop/TADDM_ServiceNow/"
#find all the files created today and only the CSV ones.
    find $sourceFolderName -type f -mtime 0 -name '*.csv'|
    while read filename
        do
             # TODO: Find only those files that we care about
             cp  $filename $targetFolderName

        done
    #targetFileName=${filename%_*}|sed 's#.*/##';
    #Rename the files now, removing the timestamp from underscore, so that it is ready to consume.
    for filename in $targetFolderName*; do

         mv -v "${filename}" ${filename%_*}.`echo "${filename}" | awk -F. '{print $2}'`

    done
abhididdigi
  • 361
  • 4
  • 16
  • `while read filename` trims trailing whitespace and processes backslash-escape sequences... and won't work at all with filenames with newlines. `find ... -print0 | while IFS= read -r -d '' filename` is the better approach. – Charles Duffy Nov 03 '14 at 19:22
  • ...but even if you aren't willing to use NUL-delimited streams, at least use `while IFS= read -r filename`. – Charles Duffy Nov 03 '14 at 19:22
  • Also, quote expansions. `find "$sourceFolderName"`. `for filename in "$targetFolderName"/*`. `cp -- "$filename" "$targetFolderName"`. – Charles Duffy Nov 03 '14 at 19:23
  • Also, no need to use awk for something the shell can do built-in. Much more efficient to use parameter expansion: `basename=${filename#*.}; basename=${filename%%.*}` if you want to take anything after the first `.` but before the second. – Charles Duffy Nov 03 '14 at 19:25