1850495 Members
2611 Online
104054 Solutions
New Discussion

Re: Moving files

 
bbutler3295_1
Occasional Contributor

Moving files

i Need to move files into sub folders by date time stamp is that posible. im running HPUX 11.11

I have thousands of files in one directory, and i want to sort them by month in sub folders to that its a little easyer on the eye's, can anyone help me ?


Thanks
7 REPLIES 7
Sajid_1
Honored Contributor

Re: Moving files

You can do a find command with cpio:
# cd dir_name
# find . -depth -atime +10 | cpio -pdlmuva /dest_dir

This will copy all directories and files under dir_name which was not accessed for last 10 days to the dest_dir

For sorting, you can use sort command:
# man sort
learn unix ..
MANOJ SRIVASTAVA
Honored Contributor

Re: Moving files

Hi


there is a one more way

cd / test ( direcorty where you ahve files )

where you have the files

ls -l | awk '{id ($6=="Jan") print $NF }' > January


and so on till you get 12 files with the file names of each month , at this point you can check the total sum to ensure u got everything.


now run the following

for i in Jan Feb Mar Apr .....
do
mkdir $i02
cat $i | while read file_name
do
cp /test/filename /test/$i02/
done
done


you will definately have to test it ,


Manoj Srivastava
Rodney Hills
Honored Contributor

Re: Moving files

Here is a perl routine that will look at all files under the current directory and will create a subdirectory (if not already created) based on the month of the modification time of the file and then move the file to that subdirectory.

@mths=qw/jan feb mar apr may jun jul aug sep oct nov dev/;
opendir(DIR,".");
while($fn=readdir(DIR)) {
next unless -f $fn;
($mtime)=(stat $fn)[9];
($mthn)=(localtime($mtime))[4];
$mth=$mths[$mthn];
mkdir $mth,0777 unless -e $mth;
die "$mth is not a directory" unless -d $mth;
system("mv $fn $mth");
}

Hope this helps...

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Moving files

# rm /tmp/olderthan
# touch -m -t 200208031230.01 /tmp/olderthan
# ls -l /tmp/olderthan
-rw-rw-rw- 1 root sys 0 Aug 3 12:30 /tmp/olderthan
# find /tmp ! -newer /tmp/olderthan -exec mv {} /newdirectorypath \;


TOUCH format

YYYYMMDDHHMM.SS

live free or die
harry
Live Free or Die
A. Clay Stephenson
Acclaimed Contributor

Re: Moving files

One thing that you do need to make a decision on is which timestamp. mtime - time of last file contents modification? atime - time of last file access? or ctime - time of last inode change (chmod, chown)? You should be aware that there is no way to know (except by accident) when a file was created. That data is not carried in the inode.

After you move the file, you probably want to do a touch so that the original datestamps are preserved.
If it ain't broke, I can fix that.
Timo Ruiter
Advisor

Re: Moving files

Hi,

In order to preserve all file times you could do a cp(1) / rm(1) command pair instead of a mv. This is of course much slower than using mv(1) but nothing else needs to be done.

Of course you will need the -p option of the cp(1) command (i.e. preserve file times and ownership).

Die captive, or just live,
Timo
Confucius say: he who runs through forrest in straight line will hit tree
harry d brown jr
Honored Contributor

Re: Moving files

Timo,

"mv" will preserve times, see below:

# ls -l sd_ipd_acl.2096
-rw-r--r-- 1 root sys 55 Dec 12 2001 sd_ipd_acl.2096
# mv sd_ipd_acl.2096 abc
# mv abc sd_ipd_acl.2096
# ls -l sd_ipd_acl.2096
-rw-r--r-- 1 root sys 55 Dec 12 2001 sd_ipd_acl.2096
# mv sd_ipd_acl.2096 /var
# mv /var/sd_ipd_acl.2096 /tmp
# ls -l sd_ipd_acl.2096
-rw-r--r-- 1 root sys 55 Dec 12 2001 sd_ipd_acl.2096
# ls -la sd_ipd_acl.2096
-rw-r--r-- 1 root sys 55 Dec 12 2001 sd_ipd_acl.2096
# ls -lu sd_ipd_acl.2096
-rw-r--r-- 1 root sys 55 Sep 2 06:23 sd_ipd_acl.2096
# date
Mon Sep 2 06:40:13 EDT 2002
#

live free or die
harry
Live Free or Die