Reputation: 31
I am running this command in one of my many subdirectories:
ls *.rst -C1 -t | awk 'NR>1' | xargs rm
Essentially, this lists all files that end with .rst and I sort them based on the time they were created and I want only the most recently created file. I then delete the rest of the *.rst files.
I have 200 directories that I need to execute this command in. I have tried using the find command to pass the location of the directories to this command but I have not been successful. All of these directories contain the files inputs.in. I have tried:
find . -name inputs.in | xargs ls *.rst -C1 -t | awk 'NR>1' | xargs rm
but I believe since the input to the ls *.rst bit is the full path including the file name, it has not been working.
I'm sure it's quick fix but your help and comments would be greatly appreciated. I would like to run this command from the parent directory. Thanks!
Upvotes: 2
Views: 302
Reputation: 33685
If you have GNU Parallel installed:
find . -name inputs.in | parallel "cd {//} && ls *.rst -C1 -t | awk 'NR>1' | xargs rm"
If it is not packaged for your system, this should install it in 10 seconds:
(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash
To learn more: Watch the intro video for a quick introduction: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
Walk through the tutorial (man parallel_tutorial). You command line will love you for it.
Upvotes: 0
Reputation: 97948
Some ugly way:
find . -name inputs.in | xargs dirname | \
xargs -I{} -n 1 find {} -name '*.rst' | xargs ls -C1 -t | \
awk 'NR>1' | xargs rm
ls with wildcard will fail because globbing happens before even find is executed.
Upvotes: 1