kizer Posted January 8, 2016 Share Posted January 8, 2016 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. 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. Quote Link to comment
kizer Posted January 9, 2016 Author Share Posted January 9, 2016 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 Quote Link to comment
itimpi Posted January 9, 2016 Share Posted January 9, 2016 Have you tried using backquotes instead of the $(...) construct? Quote Link to comment
jphipps Posted January 9, 2016 Share Posted January 9, 2016 You can pass the list from find to xargs and do a move, such as: find . -print | xargs mv /somefolder/. But I think it has problems with spaces in file names, because I think xargs parses by space... Spaces have always been a problem to deal with in file names.. Quote Link to comment
jphipps Posted January 9, 2016 Share Posted January 9, 2016 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.. Quote Link to comment
kizer Posted January 9, 2016 Author Share Posted January 9, 2016 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 Quote Link to comment
BRiT Posted January 9, 2016 Share Posted January 9, 2016 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. Quote Link to comment
kizer Posted January 9, 2016 Author Share Posted January 9, 2016 Brit Thanks. Kinda makes sense. lol Not that I don't trust people just kinda made me a little nervous not knowing what its doing just incase of a topo. Quote Link to comment
Squid Posted January 9, 2016 Share Posted January 9, 2016 Not that I don't trust people just kinda made me a little nervous not knowing what its doing just incase of a topo. Topo? Spanish for Mole? If I had a mole on my keyboard I'd be nervous too Quote Link to comment
kizer Posted January 9, 2016 Author Share Posted January 9, 2016 Typo. Sorry I haven't coded for years and apparently I haven't typed or used a dictionary either. lol Re-learning how to use find again too. Totally forgot about it. Quote Link to comment
kizer Posted January 9, 2016 Author Share Posted January 9, 2016 Hmmm, doesn't appear that find gives you an oldest search string. Quote Link to comment
BRiT Posted January 9, 2016 Share Posted January 9, 2016 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/ Quote Link to comment
jphipps Posted January 9, 2016 Share Posted January 9, 2016 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... Quote Link to comment
kizer Posted January 10, 2016 Author Share Posted January 10, 2016 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. Quote Link to comment
JonathanM Posted January 10, 2016 Share Posted January 10, 2016 Brit Thanks. Kinda makes sense. lol Not that I don't trust people just kinda made me a little nervous not knowing what its doing just incase of a topo. I thought you did that on purpose, misspelling typo to make a point. Apparently you didn't do it on purpose, which makes it even funnier. Quote Link to comment
Recommended Posts
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.