January 8, 201610 yr 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.
January 9, 201610 yr Author 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
January 9, 201610 yr 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..
January 9, 201610 yr 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..
January 9, 201610 yr Author 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
January 9, 201610 yr 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.
January 9, 201610 yr Author 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.
January 9, 201610 yr 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
January 9, 201610 yr Author 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.
January 9, 201610 yr 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/
January 9, 201610 yr 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...
January 10, 201610 yr Author 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.
January 10, 201610 yr 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.
Archived
This topic is now archived and is closed to further replies.