I have a set of folders (let's assume at /home/20161, /home/20162, /home/20163) which have been timestamped already. at the end of the day, I'd like to collect all of them and move to a main directory, timestamped, at the same path (/home/Main(date of today) including the aforementioned folders) using a shell script.

How can I do this?

Any kind of help is appreciate it

I use this script to time stamp the files into separate folders but don't know how to continue it for folders:


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" ;

for filename in Webcam.mp4; do
    mv "$filename" "$foldername"
    echo "$filename $foldername";

  • 1
    Um . . . why do you create directories in /home ? It's typically for user's personal directories , not for storing files. Also, this is a very simple question. Have you tried anything by yourself so far ? Why or why not ? – Sergiy Kolodyazhnyy Jan 9 '16 at 18:16
  • I updated the post, I do this for the files but afterwards I'd like to do the same for the created directories – Sina Sh Jan 9 '16 at 18:35
  • /home/ was just an example. the right path is included in updated post – Sina Sh Jan 9 '16 at 18:38

mv command has -t flag , synonym for --target-directory=DIRNAME.

 -t, --target-directory=DIRECTORY
              move all SOURCE arguments into DIRECTORY

Basic idea is that you can specify destination to all the items, like so: mv -t DESTINATION ITEM1 ITEM2 ITEM3. . .

So the two step process would be create the Main timestamped directory Main_$DATE and then mv all the folders you want. Like so,

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 
xieerqi@eagle:~/testdir$ ls Main_20160109_114336/
20161  20162  20163

Of course you could put everything into a small script or even a bash function inside .bashrc. Something like

function collectStuff 
  DATE=$(date +%Y%m%d_%H%M%S)

   mv -t "$MAIN"  "$@"


"$@" will expand all the command line argumens, so that way you could run something like collectStuff dirname1 dirname2 dirname3

  • Great! it worked like a charm! thanks a lot :-) – Sina Sh Jan 9 '16 at 19:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.