2014-11-03 53 views
0

我试图得到一个例子工作的文件名:仅获得使用shell脚本

这里是我想要做的事:

一)有7个文件的文件夹与名称和时间戳附加。

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

我想先a)将即将重命名的文件重命名为新文件夹,然后重命名当前文件。

我试过看Rename multiple files by replacing a particular pattern in the filenames using a shell script但该脚本不适合我。我相信我必须在那里修改一些东西,但我似乎无法得到它的工作。

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

谢谢!

+0

为了这个工作,我将不得不先遍历目录中的所有文件。下面是我使用这样做 '#!/斌/庆典 FILES = /家庭/ abhididdigi /桌面/ $中FILES TADDM/* 为f执行 回声“处理$ F文件中的脚本...“ #对每个文件执行操作。 $ f存储当前文件名 cat $ f done' 我被困在这里。这不会给我一个文件名作为输出,这给了我整个文件夹路径。一旦我得到这个工作,计划是注入上述链接中的代码,并使其工作。 – abhididdigi 2014-11-03 15:04:52

+0

将'for'换成'for home/abhididdigi/Desktop/TADDM/* do' – nu11p01n73R 2014-11-03 15:12:42

+0

我试过了,它给了我'做'不是一个文件。 #!/斌/庆典 在家庭/ abhididdigi /桌面/ TADDM F/*做 做 回声 “处理$ F文件...” #采取每个文件的行动。 $ f store当前文件名 cat $ f done 我得到了:cat:home/abhididdigi/Desktop/TADDM/*:没有这样的文件或目录 – abhididdigi 2014-11-03 15:21:15

回答

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 
+0

'while read filename'修剪尾随空格并处理反斜杠转义序列......并且根本不起作用换行符的文件名。 'find ... -print0 |而IFS =读取-r -d''文件名'是更好的方法。 – 2014-11-03 19:22:18

+0

...但即使你不愿意使用NUL分隔的流,至少使用'while IFS = read -r filename'。 – 2014-11-03 19:22:37

+0

另外,引用扩展。 '找到“$ sourceFolderName”'。 '在“$ targetFolderName”/ *'中输入文件名。 'cp - “$ filename”“$ targetFolderName”'。 – 2014-11-03 19:23:12