Shell : deleting only first 2 files in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell : deleting only first 2 files in a directory
# 1  
Old 10-12-2012
Shell : deleting only first 2 files in a directory

I have 4 files in a directory and want to delete only first 2 files only..

Code:
$ ls -ltr
total 640
-rw-r--r--   1 user   other     148779 Oct 12 10:50 file1.xls
-rw-r--r--   1 user   other     148779 Oct 12 10:50 file2.xls
-rw-r--r--   1 user   other     148779 Oct 12 10:50 file3.xls
-rw-r--r--   1 user   other     148779 Oct 12 10:50 file4.xls

I have used the below code but getting an error

Code:
$ ls -ltr file* | head -n 2 | rm
usage: rm [-fiRr] file ...

Could anyone please let me know how to delete only the first 2 files ?

Thanks in advance...

Regards,
Giridhar Sripathi
# 2  
Old 10-12-2012
Please try:-

Code:
find . -type f -exec ls -1rt {} \; | head -2 | xargs rm;

# 3  
Old 10-12-2012
Quote:
Originally Posted by bipinajith
Please try:-

Code:
find . -type f -exec ls -1rt {} \; | head -2 | xargs rm;

That is not a dependable approach. It may work for some, but not for others. For those for which it does work today, it could fail tomorrow. It could also delete files in a subdirectory.

ls is called with a single file argument. Meaning there is nothing to sort. The files will be seen by the pipeline in the order in which find visits them and find makes no guarantees about that order.

If find visits a directory before two regular files in the top-level directory, it could descend into a subdirectory and print the path to one of its regular files.

Regards,
Alister

Last edited by alister; 10-12-2012 at 01:22 PM..
# 4  
Old 10-12-2012
My bad, I totally agree!

Please post a solution.
# 5  
Old 10-12-2012
Try:
Code:
rm file[12].xls

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting files and directory's older than 3 months

I have a qnap TS259 that is running ubuntu. Have successfully setup back scripts that are initiated by cron. I would like to create a couple scrypts that would operate on the recycle bins for both drives. Just want to be able to run the script manually that would walk through both directories... (13 Replies)
Discussion started by: mackconsult
13 Replies

2. Shell Programming and Scripting

AIX system.... deleting files in remote directory after retrieving files

Hi Friends, I am new to this , I am working on AIX system and my scenario is to retrive the files from remote system and remove the files from the remote system after retreving files. I can able to retrieve the files but Can't remove files in remote system. Please check my code and help me out... (3 Replies)
Discussion started by: vinayparakala
3 Replies

3. UNIX for Dummies Questions & Answers

deleting all the files inside a directory from a relative path

I have a file inside abc/def/ghi directory. let say a.txt I need to delete this a.txt from abc itself. I have tried ls /abc/def/ghi | xargs rm -r its saying rm: a.txt non-existent also tried rm -rf /def/ghi but in vein. plz help :) (2 Replies)
Discussion started by: gotam
2 Replies

4. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

5. Shell Programming and Scripting

shell script reqd - deleting files

I have written a script that deletes files: Requirement: i need to delete the files and to know how many files are deleted i.e the count of files and even i need to display when the started time of deletion and the ending time of deletion. I need to display these two times. script: ... (2 Replies)
Discussion started by: venkatesht
2 Replies

6. Linux

deleting only directory not files

Hi Guys, I want to know wheather it is possible to delete directory not files, Example: In one directory there are 10 dirs and 100 files but my req is to delete only dirs not file Wheather it is possible ? (13 Replies)
Discussion started by: manoj.solaris
13 Replies

7. UNIX for Dummies Questions & Answers

deleting specific lines from all files in a directory

I have a directory full of text data files. Unfortunately I need to get rid of the 7th and 8th line from them all so that I can input them into a GIS application. I've used an awk script to do one at a time but due to the sheer number of files I need some kind of loop mechanism to automate... (3 Replies)
Discussion started by: vrms
3 Replies

8. Shell Programming and Scripting

deleting empty files in a directory

Hello Gurus, I want to delete only empty files (files with 0 bytes) at once from the local directory. Rightnow I need to go through all the files one by one manually and check the empty files before deleting them. Is there any unix command that finds and deletes empty files in a directory?... (5 Replies)
Discussion started by: berlin_germany
5 Replies

9. Shell Programming and Scripting

shell script: deleting files from a directory

i have the following problem: use shell script: Two directories have to be searched for files havin the same name. then delete these files from one of these directories. the directory names have to be accepted as command line arguments. what i have done till now is: 1. run a loop... (1 Reply)
Discussion started by: onlyc
1 Replies

10. UNIX for Advanced & Expert Users

Accidentally deleting directory/files

Hi, I accidentally deleted a big directory with all its sub-directories and bunch of source code files which I have been developing for about 2 years... What will I do now, how can I retrieve my files, directory hierarchy back ??? If anyone, please HELP ! ! ! ... (4 Replies)
Discussion started by: milhan
4 Replies
Login or Register to Ask a Question