创建子目录并移动文件 [英] Create subdirectory and move files

查看:54
本文介绍了创建子目录并移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目录列表,我想通过创建一组文件夹然后使用通配符来移动每个目录中的文件.

I have a list of directories and would like to move files in each of these directories by creating a set of folders and then moving with a wildcard.

到目前为止,我有:

for dir in $(ls -d */); do
    mkdir "$dir/Summary_CSV" 
    mv "*SUMMARY-ABC.csv" $dir/Summary_CSV
done

创建的目录很好(已验证),但是我始终收到一条错误消息,指出文件不存在.

The directory is created fine (verified) but I consistently get an error saying that the the files does not exist.

mv: rename SUBDIR1///*SUMMARY-ABC.csv to SUBDIR1///Summary_CSV/*SUMMARY-ABC.csv: No such file or directory

推荐答案

代替此行:

mv "*SUMMARY-ABC.csv" $dir/Summary_CSV

尝试以下操作,或者完全不使用"双引号:

Try the following or don't use " double quotes at all:

mv "${dir}"/*SUMMARY-ABC.csv $dir/Summary_CSV

您已在每个子目录(SUBDIR1/2/3等)下成功创建了文件夹"Summary_CSV",但是在复制时,您并未从每个子"目录进行复制.

You successfully created the folder "Summary_CSV" under each sub-directory (SUBDIR1/2/3 etc) but while copying, you were NOT copying from each "sub" directory.

此外,用于捕获目录的 for 循环的命令应更改为:

Also, the command for for loop for catching directory, should change from:

for dir in $(ls -d */); do

收件人:

for dir in $(ls -l|grep ^d|sed "s/[ \t][ \t]*/ /g;s/\/$//"|cut -d' ' -f9|tr '\012' ' '); do

否则,您将获得到实际文件夹的符号链接(这将导致未找到文件,因为您已经从实际文件夹或符号链接(针对该文件夹)中移出了先列出/挑选的那个文件)中移走了文件"

otherwise, you'll get symlink to an actual folder (which will result in "No file found, because you would have already moved the file from the actual folder or symlink (for that folder) whichever is listed/picked first).

完整的解决方案:在任何文件夹中尝试以下操作:

For the full solution: Try this from any folder:

giga@myLinuxMachine /tmp> rm -fr dir; mkdir dir; cd dir; mkdir sub-{dir1,dir2,dir3}; for d in `ls -1`; do touch $d/f{1,2,3} || true; done; find .;

./sub-dir1
./sub-dir1/f3
./sub-dir1/f2
./sub-dir1/f1
./sub-dir3
./sub-dir3/f3
./sub-dir3/f2
./sub-dir3/f1
./sub-dir2
./sub-dir2/f3
./sub-dir2/f2
./sub-dir2/f1
giga@myLinuxMachine /tmp/dir> : lets move files f1-3 to Summary_CSV folder inside each sub-dirN folder
giga@myLinuxMachine /tmp/dir>
giga@myLinuxMachine /tmp/dir> for dir in $(ls -l|grep ^d|sed "s/[ \t][ \t]*/ /g;s/\/$//"|cut -d' ' -f9|tr '\012' ' '); do echo "- Moving all sub-directory: ${dir}/f* files to "${dir}/Summary_CSV" folder"; mkdir ${dir}/Summary_CSV; mv ${dir}/*f* ${dir}/Summary_CSV; done; echo -e "\n\n\n- Lets see what we got after moving\n\n"; find .
- Moving all sub-directory: sub-dir1/f* files to sub-dir1/Summary_CSV folder
- Moving all sub-directory: sub-dir2/f* files to sub-dir2/Summary_CSV folder
- Moving all sub-directory: sub-dir3/f* files to sub-dir3/Summary_CSV folder



- Lets see what we got after moving


.
./sub-dir1
./sub-dir1/Summary_CSV
./sub-dir1/Summary_CSV/f3
./sub-dir1/Summary_CSV/f2
./sub-dir1/Summary_CSV/f1
./sub-dir3
./sub-dir3/Summary_CSV
./sub-dir3/Summary_CSV/f3
./sub-dir3/Summary_CSV/f2
./sub-dir3/Summary_CSV/f1
./sub-dir2
./sub-dir2/Summary_CSV
./sub-dir2/Summary_CSV/f3
./sub-dir2/Summary_CSV/f2
./sub-dir2/Summary_CSV/f1

这篇关于创建子目录并移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆