首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Shell脚本只获取文件名

问使用Shell脚本只获取文件名
EN

Stack Overflow用户
提问于 2014-11-03 14:49:03
回答 1查看 284关注 0票数 0

我试着让一个例子发挥作用:

以下是我要做的事:

( a)文件夹中有7个文件,文件的名称和时间戳都是附加的。

例如:Windows_<timestamp>.csv和Linux_<timestamp>.csv等等。

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

我试过看Rename multiple files by replacing a particular pattern in the filenames using a shell script,但这个脚本对我不起作用。我认为我必须修改里面的一些东西,但我似乎不能让它工作。

有人能帮帮我吗?我真的被困在这里了。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-03 19:19:26

代码语言:javascript
复制
#!/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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26716638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档