Shell input to remove every second file in a directory

Thread Tools Search this Thread
# 1  
Shell input to remove every second file in a directory

Hey guys,

I am looking for simple shell script, so i can remove every second file in a directory.

For example in any given directory there are 10.000 files:


After running the command, it will delete every second file in the directory. Instead of 10.000 files there are now 5000 and the dir will look like:


Now in this example I am deleting the second line, which result in only the even numbers getting deleted. But I want to be able to shrink down the directory every time I run the command.

So after running it a second time it should half the size of the directory in number of file and look like this:


I already renamed all the files so that they are sequential.

Thanks! Smilie
# 2  
So all you want to do is deleting. Why cant you do that in a single run?
# 3  
I want to delete every other / second file in a directory.

As there are 10.000+ file in this dir, I don't want to do this manually?
# 4  
What I meant was, if you keep running this script, eventually you will end up in a single file! right?

Rather, why don't you just delete all of 'em except 0001?? Smilie
# 5  
If I wanted to run the script so many times that I just would have ended up with 0001 yes I would have just RM * except the first one yes. But That is obvious not the reason why I am posting this. I need to scale down a batch of photos for a specific reason. Deleting every other file will help me with this process.

I am stuck on how to filter out every other file, to eventually delete it.

---------- Post updated at 02:36 AM ---------- Previous update was at 02:24 AM ----------

awk ‘NR%2 != 0′

should result in every second file right?

---------- Post updated at 03:11 AM ---------- Previous update was at 02:36 AM ----------

got it

ls | sed -n "p;N;" | xargs rm -r
# 6  
Its good that you found something Smilie

I hope you should be using NR%2==0, so that it result in the even line

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

2. Shell Programming and Scripting

find and replace in a directory using an input file

Hi folks, I need help to finish this script please. see below: I have an input file with all the IP address to names formated like so in a txt file addnsr1pri addnsr1sec addnsr2pri addnsr2sec (12 Replies)
Discussion started by: richsark
12 Replies

3. Shell Programming and Scripting

comparing users input to a file in another directory?

how would i go about comparing a users input to a file which is in another directory? (2 Replies)
Discussion started by: gangsta
2 Replies

4. Shell Programming and Scripting

shell to script to remove directory

Hello All I want to delete the directory in my tree structure . all the filenames like this dd-mm-yyyy Example 30-01-2011 31-01-2011 01-02-2011 I want to grep the latest last four days created directorey by not using the find command . how do i do that ? (1 Reply)
Discussion started by: kvk_shell
1 Replies

5. Shell Programming and Scripting

Remove duplicate columns in input file

hello, I have an input file which looks like this: 2 C:G 17 -0.14 8.75 33.35 3 G:C 16 -2.28 0.98 28.22 4 C:G 15 0.39 11.06 29.31 5 G:C 14 2.64 5.17 36.07 6 G:C 13 -0.65 2.05 21.94 7 C:G 11 138.96 21.64 14.40 9 C:G 27 -2.40 6.95 27.98 10 C:G 26 2.89 15.60 34.33 11 G:C... (7 Replies)
Discussion started by: linux_usr
7 Replies

6. Shell Programming and Scripting

Remove shell script and its directory

Hi, I have shell script which is located in /opt/Test/ code in is as follows #!/bin/sh rm -rf /opt/Test exit 0 when I tried to execute the above script from its location, it shows the following error rm: Cannot remove any directory in the path of the... (2 Replies)
Discussion started by: raghu.amilineni
2 Replies

7. Shell Programming and Scripting

How to create a directory structure with getting input from a file.

Hi How to create a directory structure with getting input from a file. I have file in that following lines are written. ./activemq-4.1.2/activemq-core-4.1.2.jar ./activemq-4.1.2/backport-util-concurrent-2.1.jar ./camel-1.4.0/apache-camel-1.4.0.jar ./camel-1.4.0/lib/activation-1.1.jar... (12 Replies)
Discussion started by: itsjoy2u
12 Replies

8. Shell Programming and Scripting

a remove script taken in input a file which contain a list of directories

Hi, I'm looking to delete some files from directories. I've just put in a file the location of these files. e.g: in file supprs.txt there is: /usr/host/t1.txt /etc/dev/ /home/new/files/view.c Is it possible to take this file "supprs.txt" as a parameter in a shell command ? (2 Replies)
Discussion started by: yeclota
2 Replies

9. UNIX for Dummies Questions & Answers

removing directory in an input file

Hi, How can I removed the directory in the input file? The script responsible for storing the report in an input file is this: while }" ] do echo "penetration|${penfilename}|${penfilenamedaterange}" >> ${OUT_DIR}/penrpt_emailfile.txt (( i=i+1 )) done For the penfilename: ... (2 Replies)
Discussion started by: chrysSty
2 Replies

10. Shell Programming and Scripting

input a line at the beginning of every file in a directory?

if need to input a word or anything at the beginning of every file in a directory. how do i accomplish this? say the file is named hyperten. how do i make hyperten the first line of every file in a given directory? thanks (6 Replies)
Discussion started by: Terrible
6 Replies

Featured Tech Videos