linux - How to tar the n most recent files -
i trying create script foreach directoy in folder folder, n recent files compressed.
however, having trouble multiple word files. need way wrap them in quote marks tar command knows wich each file.
here script far:
#!/bin/bash if [ ! -d ~/backup ]; mkdir ~/backup fi cd ~/folder in *; if [ -d "$i" ]; original=`pwd` cd $i echo tar zcf ~/backup/"$i".tar.gz "`ls -t | head -10`" cd $original fi done echo "backup copied in $home/backup/" exit 0
if [ ! -d ~/backup ]; mkdir ~/backup fi
you can simplify :
[[ ! -d ~/backup ]] && mkdir ~/backup
now answer question :
$ ls -t|head -10 file spaces file test.txt test test.sh $ lstfiles=""; while read; lstfiles="$lstfiles \"$reply\""; done <<< "$(ls -t|head -10)" $ echo $lstfiles "file spaces" "file" "test.txt" "test" "test.sh"
see how read command output or file content loop in bash read more details.
Comments
Post a Comment