有没有办法用grep,awk和piping进行以下操作:
考虑父文件夹A,其中包含四个文件夹B,C,D,E。每个文件夹中的图层数量都是未知的。我想在每个文件夹B,C,D,E的层次结构中找到.pdf文件。这可以通过find找到:
find . -type f -name "*.pdf"
找到文件后,我会将其移至另一个文件夹。因此,在B下找到的.pdf文件将被移动到父文件夹中名为B_NEW的新创建的文件夹,依此类推。我不知道怎么拿管道并创建文件夹并进行移动/复制!
答案 0 :(得分:1)
TempList=/tmp/ListOfParentFile.tmp
ParentFolder=A
here="$( pwd )"
cd ${ParentFolder}/..
find ${ParentFolder##*/} -print > ${TempList}
if [ $( grep -c -E -e '.pdf$' ${TempList} ) -gt 0 ]
then
mkdir "${ParentFolder##*/}${ParentFolder##*/}"
echo 'set -vx' > ${TempList}.action
sed -n '\#^\([^/]*\)/\([^/]*\)$# s##cp -fr &* \1\1/\2\2#p' ${TempList} >> ${TempList}.action
. ${TempList}.action
rm ${TempList}*
cd ${here}
fi
快速而肮脏的概念,在此添加任何安全性。在KSH / AIX中测试
<强>释强>
${ParentFolder##*/}
的 double 名称创建一个文件夹(与Reference相同)两次。使用shell功能在最后/
set -vx
是可选的,而不是文件)Reference/Subfolder
的每个行条目生成一组操作,以递归方式复制文件夹和内容。其他行将被丢弃,因此只会处理Reference文件夹中的第1级子文件夹。答案 1 :(得分:0)
这应该有效
while read file;do
dir="New_${file#*/}" #strip to first / and add "New_"
dir="${dir%/*}" #strip off filename
if [[ ! -d "$dir" ]];then #check directory exists or not
echo "creating directory structure $dir"
mkdir -p "$dir" || (echo "failed to create $dir.Aborting for this file" && continue) # make the directory and continue to next file if this fails
fi
echo "copying $file -> $dir/"
cp "$file" "$dir/" || (echo "failed to copy $file.Aborting for this file" && continue) #Copy the file to newly created dir
done< <(find . -type f -name "*.pdf") #find the files
$find . -type f -name "*.pdf"
./A/4.pdf
./A/3.pdf
./A/1.pdf
./A/2.pdf
./C/4.pdf
./C/3.pdf
./C/1.pdf
./C/2.pdf
./B/7.pdf
./B/8.pdf
./B/6.pdf
./B/5.pdf
./D/7.pdf
./D/8.pdf
./D/6.pdf
./D/5.pdf
$./Scipt
creating directory structure New_A
copying ./A/4.pdf -> New_A/
copying ./A/3.pdf -> New_A/
copying ./A/1.pdf -> New_A/
copying ./A/2.pdf -> New_A/
creating directory structure New_C
copying ./C/4.pdf -> New_C/
copying ./C/3.pdf -> New_C/
copying ./C/1.pdf -> New_C/
copying ./C/2.pdf -> New_C/
creating directory structure New_B
copying ./B/7.pdf -> New_B/
copying ./B/8.pdf -> New_B/
copying ./B/6.pdf -> New_B/
copying ./B/5.pdf -> New_B/
creating directory structure New_D
copying ./D/7.pdf -> New_D/
copying ./D/8.pdf -> New_D/
copying ./D/6.pdf -> New_D/
copying ./D/5.pdf -> New_D/
find . -type f -name "*.pdf"
./A/4.pdf
./A/3.pdf
./A/1.pdf
./A/2.pdf
./C/4.pdf
./C/3.pdf
./C/1.pdf
./C/2.pdf
./New_C/4.pdf
./New_C/3.pdf
./New_C/1.pdf
./New_C/2.pdf
./New_A/4.pdf
./New_A/3.pdf
./New_A/1.pdf
./New_A/2.pdf
./B/7.pdf
./B/8.pdf
./B/6.pdf
./B/5.pdf
./New_B/7.pdf
./New_B/8.pdf
./New_B/6.pdf
./New_B/5.pdf
./New_D/7.pdf
./New_D/8.pdf
./New_D/6.pdf
./New_D/5.pdf
./D/7.pdf
./D/8.pdf
./D/6.pdf
./D/5.pdf