bash - Filter before copy in shell -



bash - Filter before copy in shell -

i have directory files jan 2014 nov 2014. sadly files have no dates in name, way can know month file belongs using ls -lrt , checking month there.

so have re-create these files location based on month file created.

ls -lrt | grep oct

will give me oct files.

now, tried

ls -lrt | grep oct | cp * /../../oct2014

this didn't work. there anyway can perform such task?

edit: give thanks much replying such great answers finish explanation. humbled. seek these tomorrow morning.

further, files dealing not have special characters in names. again.

this 1 way

ls -ltr | awk '/oct/{print $nf}' | xargs -i{} cp {} newdir

or utilize find

find . -type f -newermt "oct 01" ! -newermt "oct 31" | xargs -i{} cp {} newdir

more robust find (credit broslow)

find . -type f -newermt "oct 01" ! -newermt "oct 31 23:59:59" -print0 | xargs -0 -i{} cp {} newdir

if need accurate thing in world ever

find . -type f -newermt "oct 01" ! -newermt "oct 31 23:59:59.9999999999999999999999999999999999999999999999999999999999999" -print0 | xargs -0 -i{} cp {} newdir note

just thought add together whilst parsing ls in situations unsafe (due not beingness great @ showing special characters, , programs terminate arguments spaces , newlines), if aware filenames in target folder regular names no special characters or spaces parsing ls work fine. . names of files @ discretion of user though, in cases names can alter improve utilize find solution.

bash shell filter copy cp

Comments

Popular posts from this blog

c# - ASP.NET MVC Sequence contains no matching element -

java - Parsing XML, skip certain tags -

rest - How to invalidate user session on inactivity in a stateless server? -