2014-11-03 37 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