abhididdigi
abhididdigi

Reputation: 371

Get only the Filename using a Shell Script

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!

Upvotes: 0

Views: 403

Answers (1)

abhididdigi
abhididdigi

Reputation: 371

#!/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

Upvotes: 1

Related Questions