编写linux脚本以将文件从一个文件夹移动到另一个文件夹。但是,它需要首先显示文件的属性,例如大小,创建日期,名称等,然后询问用户是否要复制它。
我可以批量复制,但不知道如何查看一个文件的属性然后询问用户是否要复制它,然后移动到文件夹中的下一个文件。
非常感谢任何帮助。
我会复制我已经完成的代码,但是没有一个与问题相关,我到目前为止已经处理了两个文件夹参数(源和目标)并创建了目标文件夹(如果指定的那个)不存在。
总结:
程序将文件从一个文件夹逐个复制到另一个文件夹
对于每个文件,需要显示属性
然后询问用户是否要复制文件
复制文件,然后移到下一个文件(猜测文件夹中的文件数可以使用内置的bash参数计算)
谢谢!
康纳
答案 0 :(得分:1)
Dialog命令是你的朋友。而不是试图"得到"文件属性,只需使用ls -al '$filename'
答案 1 :(得分:1)
以下脚本可以使用:
dir=$1
newdir=$2
for file in $dir/*
do
filesize=$(stat -f%z $file) # stat command finds size of file in bytes
filename=$(basename $file)
echo "Name of file: $filename"
echo "File size: $filesize bytes"
ls -l $file #shows permisions, parent directory, last modification date...
read -r -p "Would you like to copy file?:" answer
if [[ $answer =~ ^(yes|y| ) ]] # checks possible user entries
then
cp $file $newdir/$filename #copies file from original dir to new dir
else
echo "file not copied"
fi
done
对于读取用户输入的read
命令,这里是对手册页的描述:
-p prompt
Display prompt, without a trailing newline, before attempting
to read any input. The prompt is displayed only if input is coming from a
terminal.
-r
If this option is given, backslash does not act as an escape character.
The backslash is considered to be part of the line. In particular, a backslash-newline
pair may not be used as a line continuation.
脚本运行如下:
./script original new
其中original
是要读取的目录,new
是您希望将文件复制到的目录。