使用Shell脚本仅获取文件名

时间:2014-11-03 14:49:03

标签: shell

我试图让一个例子工作:

以下是我要做的事情:

a)文件夹中有7个文件,其中附有名称和时间戳。

示例:Windows_<timestamp>.csvLinux_<timestamp>.csv等等。

我想首先a)将我即将重新命名的文件移动到新文件夹中,然后重命名当前文件。

我试过看Rename multiple files by replacing a particular pattern in the filenames using a shell script,但那个剧本并不适合我。我相信我必须在那里修改一些东西,但我似乎无法让它发挥作用。

任何人都可以帮助我吗?我真的被困在这里了。

谢谢!

1 个答案:

答案 0 :(得分: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