|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
|||
|
|||
|
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
|
||||
|
||||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|