Assistance for sorting files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assistance for sorting files
# 1  
Old 02-05-2013
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. Smilie
# 2  
Old 02-05-2013
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.
# 3  
Old 02-05-2013
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
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

These 2 Users Gave Thanks to balajesuri For This Post:
# 5  
Old 02-05-2013
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 09:37 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk assistance - Comparing 2 csv files

Hello all, I have searched high and low for a solution to this, many have come really close but not quite what I'm after. I have 2 files. One contains GUID's, for example: 8121E002-96FE-4C9C-BC5A-6AFF20DACECD 84468F30-F3B7-418B-81F0-0908E80792BF A second file, contains a path to the... (8 Replies)
Discussion started by: tirmUK
8 Replies

2. Shell Programming and Scripting

Assistance with an awk code to split files but keep the header

---------- Post updated at 11:48 AM ---------- Previous update was at 11:46 AM ---------- Hello all I have an awk code that successfully creates separate text files based on the first six letters of the second field. What it doesn't do is preserve the header into each resulting file. ... (6 Replies)
Discussion started by: colecandoo
6 Replies

3. Shell Programming and Scripting

Complex data sorting in excel files or text files

Dear all, I have a complex data file shown below,,,,, A_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 C_ABCD_13208 0 0 4.16735 141044 902449 1293900 168919 A_ABCDEF715 52410.9 18598.2 10611 10754.7 122535 252426 36631.4 C_DBCDI_1353 0... (19 Replies)
Discussion started by: AAWT
19 Replies

4. Shell Programming and Scripting

Need Assistance with gzipping files with same names

Hello, I have an issue on a webserver where I have a perl script rotating and moving the logs to an archive directory where they are gzipped. This is working fine. The problem is when there is tracing enabled on my webserver where the tracelogs are rotated whenenver they reach 100Meg and they... (2 Replies)
Discussion started by: techwiz45
2 Replies

5. UNIX for Dummies Questions & Answers

Assistance with combining, sorting and saving multi files into one new file

Good morning. I have a piece of code that is currently taking multiple files and using the CAT.exe command to combine into one file that is then sorted in reverse order based on the 3rd field of the file, then displayed on screen. I am trying to change this so that the files are being combined into... (4 Replies)
Discussion started by: jaacmmason
4 Replies

6. Shell Programming and Scripting

script assistance needed - create an archive of INI files

First and foremost - me != unix bubba. Here is the situation. We have a box with data AND settings in the same directory path. (Data files aren't in the SAME directories as settings.) I need a script that generates a tarred-up archive of only the INI files with the directory structure. We... (2 Replies)
Discussion started by: hindesite
2 Replies

7. Shell Programming and Scripting

Assistance pls - pipe error: Too many open files in system

When I run a bash script in the customer system, it throws the warning and script exits Exec '/root/sample.sh' @ hostname-- OK (warn) /root/sample.sh: pipe error: Too many open files in system /root/sample.sh: n + : syntax error: operand expected (error token is " ") Exec... (5 Replies)
Discussion started by: vidhyamirra
5 Replies

8. Shell Programming and Scripting

Need assistance with simple shell script to organize files. [Code attached]

I need some help with this shell script for class. All it does is organize your files. It works, but in the log file, it needs to show the new filepaths of the moved files. Heres my log of my output: Starting to organize... movie2.wmv --> movie3.mov --> movie1.mpg --> song1.mp3 --> ... (3 Replies)
Discussion started by: ryandamartini
3 Replies

9. Shell Programming and Scripting

Assistance with regex and config files

I am trying to write a shell script that will such in data from a config file. The script should mount device nodes that are contained in a config file in the following format: # filesystem type # read/write #device # Mount Point xfs w ... (1 Reply)
Discussion started by: pryker
1 Replies

10. Windows & DOS: Issues & Discussions

Urgent Need for Assistance: Triggering Windows bat files from UNIX

Hello, Is there a way to trigger a Windows bat file or program on a different machine from a different UNIX server using KSC file? I hope you can assist me with this. Thanks! (0 Replies)
Discussion started by: punyenye
0 Replies
Login or Register to Ask a Question