Code Question Move Oldest Folder


kizer

Recommended Posts

I'm looking for some code to move the oldest directory and all of its contents from a given path to another location.

For Example

 

Move the oldest <Movie> from a disk share to another disk share including all files. I'd have no problem running it manually or making a cron job out of it. BONUS I'd seriously like if possible a way to determine how much space is left on Source and then move when a Threshold is met, but its not super important, but it would be so nice.  :D

 

Disk3/

..Movies

...All

....<Transformers>

....<Star Trek>

....<Star Wars>

 

Disk/5

..Movies

...All

 

I have some code or a Work flow I use on my Windows PC and I often copy everything to Disk3 that is Movie related and then I often Archive older things to other discs on my Server. I suppose for simplicity I could just change the path now and then to other drives, but I'd have to dig thru all my work and change things.

 

If found code examples to delete or move oldest file, but that's now cutting it for my needs. If somebody could throw me a bone or give me some direction I have zero problems trying to hack it together. ITs just been so long since I've done much coding.

Link to comment

Currently this delivers the oldest directory for me

ls -lt /mnt/disk3/Movies/All/ | tail -n 1

 

However I just can't figure out how to pass that to a mv command when normally I'd type

mv foldername /destination

 

I tried to wrap it like so, but nope. lol

mv $(ls -lt /mnt/disk3/Movies/All/ | tail -n 1) /mnt/disk5

 

Link to comment

Looks like this will work:

 

find . -type f -print0 | xargs -0 mv -t ./folder/

 

Did find this on stackoverflow for using ls:

 

/bin/ls | tr '\n' '\0' | xargs -0 -i% mv % /path/to/destdir/

 

Similar to the -print0 that uses a null instead of newline or space to seperate file names..

Link to comment

itimpi

Gives me a

 

Can not Stat and no directory found. Hmmmmmm

 

jphipps

I don't use find or xargs much so I'm trying to tear apart what you typed and use it in my applilcation. Just kinda confusing me and kinda nervous to blind post in console to see what it does. Lol

Link to comment

itimpi

Gives me a

 

Can not Stat and no directory found. Hmmmmmm

 

jphipps

I don't use find or xargs much so I'm trying to tear apart what you typed and use it in my applilcation. Just kinda confusing me and kinda nervous to blind post in console to see what it does. Lol

 

Create your own temporary working directory to try it out on. You could also slightly change it to use "echo" instead of a command like mv to see what it would do.

 

find is just a more advanced directory or file listing mechanism.

Xargs merely breaks up what is passed to it into usable chunks to be passed to the command you specify.

 

Link to comment

ls -tr /dir | head -1 | xargs echo mv /dest -t

 

# brief description of each step

ls -tr /dir # this will do a directory listing based on reverse [-r] date [-t].

head -1  # this will take the first line of output

xargs # this will split the input as an argument and execute the specified command

echo mv /dest -t # this will echo the command to standard output

mv /dest -t # this will move to the /dest directory the source directory [-t]

 

Example:

# ls -al /mnt/cache/downloads/

total 4

drwxrwxrwx 9 nobody users  116 Aug 18 22:14 ./

drwxrwxrwx 6 nobody users  60 Aug 19 00:31 ../

drwxrwxrwx 8 nobody nobody 113 Jan  1 20:04 extracted/

drwxrwxrwx 2 nobody nobody  64 Jan  7 02:39 import/

drwxrwxrwx 2 nobody nobody  6 Jan  3  2015 imported/

drwxrwxrwx 2 nobody nobody  6 Jan  1 20:04 incomplete/

drwxrwxrwx 2 nobody nobody  6 Jan  3  2015 irc/

drwxrwxrwx 2 nobody nobody  6 Jan  3  2015 logs/

-rw-r----- 1 nobody users    3 Dec 31 21:34 nzbget.lock

drwxrwxrwx 2 nobody nobody  6 Jan  9 13:44 tmp/

 

ls -tr /mnt/cache/downloads/ | head -1 | xargs echo mv /dest -t

mv /dest -t logs/

 

 

 

Link to comment

ls -tr /dir | head -1 | xargs echo mv /dest -t

 

# brief description of each step

ls -tr /dir # this will do a directory listing based on reverse [-r] date [-t].

head -1  # this will take the first line of output

xargs # this will split the input as an argument and execute the specified command

echo mv /dest -t # this will echo the command to standard output

mv /dest -t # this will move to the /dest directory the source directory [-t]

 

Example:

# ls -al /mnt/cache/downloads/

total 4

drwxrwxrwx 9 nobody users  116 Aug 18 22:14 ./

drwxrwxrwx 6 nobody users  60 Aug 19 00:31 ../

drwxrwxrwx 8 nobody nobody 113 Jan  1 20:04 extracted/

drwxrwxrwx 2 nobody nobody  64 Jan  7 02:39 import/

drwxrwxrwx 2 nobody nobody  6 Jan  3  2015 imported/

drwxrwxrwx 2 nobody nobody  6 Jan  1 20:04 incomplete/

drwxrwxrwx 2 nobody nobody  6 Jan  3  2015 irc/

drwxrwxrwx 2 nobody nobody  6 Jan  3  2015 logs/

-rw-r----- 1 nobody users    3 Dec 31 21:34 nzbget.lock

drwxrwxrwx 2 nobody nobody  6 Jan  9 13:44 tmp/

 

ls -tr /mnt/cache/downloads/ | head -1 | xargs echo mv /dest -t

mv /dest -t logs/

 

I think you might see an issue with file names with spaces.  Usually xargs will split on a space.  the -0 arg on xargs will use a null to split on, so if you can feed it a list of null delimited filenames it will handle spaces.  i think if you add in the tr to tanslate the new line to null you would be good to go..

 

 

You can use find with a maxdepth and a type -d ( directory ) but it might get a bit more complex than it usihg the ls...

Link to comment

Went old school and tinkered with what I used to use and I can plug in Variables later if I want. I just don't know how to launch it based on drive space, but one problem at a time.  ;) Nothing Fancy, but I can drop into Cron and have it run once a week or daily or whatever I choose.

 

Anyways thanks for all the help guys.

 

#!/bin/sh

 

MoveSearch="`ls -t /mnt/disk3/Movies/All/ | tail -n 1`"

Sets MoveSearch Variable with the location and what to search for. In this case I said search the Disk3 Drive and give me the oldest folder

 

mv /mnt/disk3/Movies/All/$MoveSearch /mnt/disk4/Movies/All/

Drops in the Variable MovieSearch and then launches the mv command and where I want it moved to.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.