我有一组文件夹(假设在、、/home/20161
),它们已经有时间戳。在一天结束时,我想使用 shell 脚本收集所有这些文件夹并移动到主目录(带有时间戳、位于同一路径()的(包括前面提到的文件夹)。/home/20162
/home/20163
/home/Main(date of today
我怎样才能做到这一点?
任何帮助都值得感激
我使用此脚本将文件加盖时间戳并放入单独的文件夹中,但不知道如何继续对文件夹进行此操作:
#!/bin/bash
cd /home/sina/Desktop/Recordings/
shopt -s nullglob # this line is so that it does not compain when no logfiles are found
for filename in Screen.mkv; do # Files considered are the ones startign with test and ending in .log
foldername=$(date +%Y%m%d_%H%M%S); # Create Folder based on date of time of recording
mkdir -p "$foldername" # -p so that we dont get "folder exists" warning
mv "$filename" "$foldername"
echo "$filename $foldername" ;
done
for filename in Webcam.mp4; do
mv "$filename" "$foldername"
echo "$filename $foldername";
done
答案1
mv
命令有-t
标志,的同义词--target-directory=DIRNAME
。
-t, --target-directory=DIRECTORY
move all SOURCE arguments into DIRECTORY
基本思想是您可以为所有项目指定目的地,如下所示:mv -t DESTINATION ITEM1 ITEM2 ITEM3. . .
因此,两步过程将创建主时间戳目录 Main_$DATE
,然后创建mv
所需的所有文件夹。像这样,
xieerqi@eagle:~/testdir$ DATE=$(date +%Y%m%d_%H%M%S)
xieerqi@eagle:~/testdir$ mkdir Main_$DATE
xieerqi@eagle:~/testdir$ ls
20161 20162 20163 Main_20160109_114336
xieerqi@eagle:~/testdir$ mv -t Main_20160109_114336/ 20161/ 20162/ 20163/
xieerqi@eagle:~/testdir$ ls
Main_20160109_114336
xieerqi@eagle:~/testdir$ ls Main_20160109_114336/
20161 20162 20163
当然,你可以把所有内容放入一个小脚本中,甚至放入一个 bash 函数中.bashrc
。比如
function collectStuff
{
CENTRALDIR="/home/sina/Desktop/Recordings"
DATE=$(date +%Y%m%d_%H%M%S)
MAIN="$CENTRALDIR"/"Main_$DATE"
mv -t "$MAIN" "$@"
}
"$@"
将扩展所有命令行参数,这样你就可以运行类似collectStuff dirname1 dirname2 dirname3