Assistance for sorting files | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Assistance for sorting files

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 02-05-2013
chewku chewku is offline
Registered User
Join Date: Sep 2012
Last Activity: 12 September 2013, 4:00 AM EDT
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Assistance for sorting files

Hi there,

I have tried using the "find" command to do this but to no avail as the "find -mtime" command I used descend to the directories from my current working directory.

Say in "directoryA" has multiple files and those files are created on a daily basis.

Under "directoryB", there are individual directories labelled 01, 02, 03 ....... 31.

What I am trying to achieve here is a BASH script that will perform a loop in "directoryA" sorting those 30 individual files based on modified date and then move those files into their respective directories under "directoryB". For instance, a file named "a1.txt" in "directoryA" that was created on the 1st of Jan is to be moved to the "01" directory under "directoryB".

Any help would be much appreciated.
Sponsored Links
Old 02-05-2013
jim mcnamara jim mcnamara is offline Forum Staff  
Join Date: Feb 2004
Last Activity: 22 December 2014, 6:58 PM EST
Location: NM
Posts: 10,313
Thanks: 297
Thanked 823 Times in 765 Posts
This can be done. But the way you have put your question together leaves too much guesswork on our part.

Please give us actual sample input - filenames - and actual expected results.
Sponsored Links
Old 02-05-2013
chewku chewku is offline
Registered User
Join Date: Sep 2012
Last Activity: 12 September 2013, 4:00 AM EDT
Posts: 4
Thanks: 3
Thanked 0 Times in 0 Posts
Thanks for the swift response.

My current working directory:


has the following files:

a1.txt created on 1st of Jan
a2.txt created on 2nd of Jan
a3.txt created on 3rd of Jan
a4.txt created on 4th of Jan
a5.txt created on 5th of Jan
a6.txt created on 6th of Jan
a7.txt created on 7th of Jan
a8.txt created on 8th of Jan
a9.txt created on 9th of Jan

Expected results:

a1.txt (/home/dirA/a1.txt) to end-up in this directory "/home/dirB/01/" and the long listing of that file is "/home/dirB/01/a1.txt"
Old 02-05-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
Join Date: Apr 2009
Last Activity: 16 December 2014, 10:34 AM EST
Location: India
Posts: 1,837
Thanks: 25
Thanked 527 Times in 506 Posts

#! /bin/bash


for x in $fromDir/*
    new=$(stat -c%y $x | awk -F'[ -]' '{print $3}')
    mkdir $toDir/$new
    cp $fromDir/$x $toDir/$new/

The Following 2 Users Say Thank You to balajesuri For This Useful Post:
chewku (02-07-2013), jim mcnamara (02-05-2013)
Sponsored Links
Old 02-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Join Date: Nov 2008
Last Activity: 22 December 2014, 10:28 AM EST
Location: Amsterdam
Posts: 9,698
Thanks: 301
Thanked 2,492 Times in 2,231 Posts
stat is not available everywhere. This may be an alternative:

cd "$fromDir"
LANG=C ls -nl |                               # use POSIX locale for ls command to get universal output
  read                                        # discard the "total" line
  while read perm x x x x x day x file        # Read the next entry and catch $1, $7 and $9 and further.
    case $perm in (-*)                        # if it is a file
      new=$(printf "%02d" "$day")             # pad a zero to day if need be
      mkdir "$toDir/$new" 2>/dev/null         # create the target directory; ignore error
      cp -p "$file" "$toDir/$new"

Last edited by Scrutinizer; 02-05-2013 at 09:37 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
chewku (02-07-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Assistance with gzipping files with same names techwiz45 Shell Programming and Scripting 2 02-04-2011 10:24 AM
Assistance with combining, sorting and saving multi files into one new file jaacmmason UNIX for Dummies Questions & Answers 4 01-31-2011 12:26 PM
script assistance needed - create an archive of INI files hindesite Shell Programming and Scripting 2 08-12-2010 01:15 AM
Assistance pls - pipe error: Too many open files in system vidhyamirra Shell Programming and Scripting 5 03-01-2010 10:08 AM
Assistance with regex and config files pryker Shell Programming and Scripting 1 03-04-2008 03:11 PM

All times are GMT -4. The time now is 11:40 PM.