Shell input to remove every second file in a directory


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell input to remove every second file in a directory
# 1  
Old 07-16-2012
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:

0001.jpg
0002.jpg
0003.jpg
0004.jpg
0005.jpg
0006.jpg
0007.jpg
0008.jpg
....
1111.jpg
1112.jpg
etc.

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:

0001.jpg
0003.jpg
0005.jpg
0007.jpg
....
1111.jpg

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:

0001.jpg
0005.jpg
....
etc

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

Thanks! Smilie
# 2  
Old 07-16-2012
So all you want to do is deleting. Why cant you do that in a single run?
# 3  
Old 07-16-2012
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  
Old 07-16-2012
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  
Old 07-16-2012
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  
Old 07-16-2012
Its good that you found something Smilie

I hope you should be using NR%2==0, so that it result in the even line
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove Comment Lines From Script/Input File

Hello, I have a SAS code that predominantly has comments line and the real code like below and i want to remove ONLY THE COMMENTS from the code in the single line or spanned across multiple lines. /******************************************************************** *** This Is a Comment... (4 Replies)
Discussion started by: arooonatr
4 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 166.7.3.105 addnsr1sec 166.2.100.22 addnsr2pri 166.2.220.121 addnsr2sec 166.3.68.45... (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/test.sh code in test.sh 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/u1.java /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
Login or Register to Ask a Question