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
    #1  
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
    #2  
Old 02-05-2013
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 19 April 2014, 8:15 AM EDT
Location: NM
Posts: 10,058
Thanks: 250
Thanked 756 Times in 711 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
    #3  
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:

/home/dirA

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"
    #4  
Old 02-05-2013
balajesuri's Avatar
balajesuri balajesuri is offline Forum Advisor  
#! /bin/bash
 
Join Date: Apr 2009
Last Activity: 20 April 2014, 5:09 AM EDT
Location: India
Posts: 1,798
Thanks: 23
Thanked 516 Times in 495 Posts

Code:
#! /bin/bash

$fromDir=/home/dirA
$toDir=/home/dirB

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

The Following 2 Users Say Thank You to balajesuri For This Useful Post:
chewku (02-07-2013), jim mcnamara (02-05-2013)
Sponsored Links
    #5  
Old 02-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 20 April 2014, 3:08 PM EDT
Location: Amsterdam
Posts: 8,878
Thanks: 230
Thanked 2,141 Times in 1,922 Posts
stat is not available everywhere. This may be an alternative:


Code:
$fromDir=/home/dirA
$toDir=/home/dirB
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.
  do
    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"
    esac
  done
}


Last edited by Scrutinizer; 02-05-2013 at 08: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 09:24 AM
Assistance with combining, sorting and saving multi files into one new file jaacmmason UNIX for Dummies Questions & Answers 4 01-31-2011 11:26 AM
script assistance needed - create an archive of INI files hindesite Shell Programming and Scripting 2 08-12-2010 12:15 AM
Assistance pls - pipe error: Too many open files in system vidhyamirra Shell Programming and Scripting 5 03-01-2010 09:08 AM
Assistance with regex and config files pryker Shell Programming and Scripting 1 03-04-2008 02:11 PM



All times are GMT -4. The time now is 05:31 PM.