我是创建.sh文件(和bash脚本)的新手,我正在尝试创建一个脚本,该脚本可以读取,将副本文件重命名为具有适当名称的文件夹,但不知道如何解决这个问题。我希望你们中的一个人能为我指出正确的方向。
这是我的开始(仍然有一些错误)
#!/bin/bash
for i in 'find /temp_pdf -type f | xargs grep *.pdf'; #loop for findg all .pdf files residing in temp_pdf
do
#1234 invoice.pdf
directory=${i:0:4}; # read the first 4 chars of the file name
#cp 1234 invoice.pdf /copy/1234*
cp $i /copy/$directory*; #copy the the file to /copy/xxxx* folder
done;
也许你想做这样的事情:
SOURCE="./temp_pdf"
DESTINATION="copy"
mkdir -p "$DESTINATION"
for i in `find "$SOURCE" -type f | grep 'pdf$'`; do #loop for findg all .pdf files residing in temp_pdf
cp -v "$i" "$DESTINATION" #copy the the file to DESTINATION folder (verbose)
done
我不确定您是否要使用全局目标文件夹。如果你想这样做,你可以在循环中做。
如果我在一个目录中有 .ext 文件,我想在更改文件名时将其放在另一个目录中,我会这样做:
for file in path/to/dir1/*.ext; do
mv $file path/to/dir2/new_pref${file}new_suf
done
你可以把它放在一个名为 ren.sh 的文件中,然后
$ chmod u+x ren.sh
$ ./ren.sh
new_pref
可以是一些字符串,new_suf
可以是类似于.alt
或.new的东西。如果要删除文件名的某些部分,请使用${file%*id}
或${file##*id}
分别删除文件名开头或结尾的内容,直到并包括id
。其中id
是文件名的重复部分,如.
、_
或-
或其他东西。谷歌参数扩展以获取更多详细信息,或查看参数扩展下的"man bash"。
解决方案更简单:
$ find . -name *.pdf -exec cp ...
如果您想了解更多信息,请看这里。
编辑
更简单地说:
$ cp **/*.pdf target_directory