rm long list of files in a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rm long list of files in a directory
# 8  
Old 08-21-2009
corona688 has the right idea by getting "find" to expand the list, not shell.

This alternative method allows you to test the script without deleting anything (like corona688 version), works for any number of file in the directory, and works for filenames containing space characters:
The "." in the filename is escaped as "\." to make the point that this is not MSDOS and "." can be part of a regular expression.

We don't know if you have subdirectories containing files you don't want to delete. Otherwise generate the files list more efficiently with:
Code:
find . -type f -name \*\.txt -print


Code:
#!/bin/ksh
ls -1 *\.txt | while read filename
do
      echo "Deleting: ${filename}"
      # Uncomment next line if you are happy
      # rm "${filename}"
done

# 9  
Old 08-21-2009
try the following:
Code:
find . -name '*.txt' | xargs rm

like this xargs will take care of splitting up the list of files into acceptable chunks. it will execute much faster than running rm for every single file found (what will happen when using the -exec option) without worrying about the number of files passed onto rm.
# 10  
Old 08-21-2009
Code:
find -maxdepth 1 -name '*.txt' | xargs rm

Smilie
# 11  
Old 08-21-2009
didnt worked

didnt wrked
# 12  
Old 08-21-2009
which command isn't working?
# 13  
Old 08-21-2009
Leppie version is good. It will execute "rm" for each file like all sensible solutions. I can't see a way of testing it without deleting files.

In a modern unix, frequently executed programs are cached in memory and don't give the performance issues of 1980's unix. Similarly issuing a continuous stream of "exec" calls no longer crashes the kernel.

There are many methods to process large numbers of files, but to my mind the most important aspect is to be able to test what will happen in advance.

There is much variation in the "find" command depending on your version and vintage of unix or Linux. It always helps to state yout Operating System and preferred shell.

For example the "-maxdepth" parameter to "find" is not found in many unixes.

Last edited by methyl; 08-21-2009 at 05:40 PM.. Reason: Remove unwanted "previous edit" lines
# 14  
Old 08-21-2009
It worked

find /path/to/dir/ -iname '*.pdf' -exec echo rm '{}' ';'

This worked!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with date, create directory, move to the created directory

Hi all, i have a folder, with tons of files containing as following, on /my/folder/jobs/ some_name_2016-01-17-22-38-58_some name_0_0.zip.done some_name_2016-01-17-22-40-30_some name_0_0.zip.done some_name_2016-01-17-22-48-50_some name_0_0.zip.done and these can be lots of similar files,... (6 Replies)
Discussion started by: charli1
6 Replies

2. Shell Programming and Scripting

Copy list of files from a keyword list to another directory

Hello, I have a folder with a massive amount of files, and I want to copy out a specific subset of the files to a new directory. I would like to use a text file with the filenames listed, but can't get it to work. The thing I'm hung up on is that the folder names in the path can and do have... (5 Replies)
Discussion started by: twjolson
5 Replies

3. Shell Programming and Scripting

Need Help Moving Long List Of Files Into Directories

I am very new to BASH and I am having difficulties moving a long list of image files into similarly named directories. I've been trying to come with a script all night and no luck. Here is what my list of files looks like: DSC_0059_01.jpg DSC_0059_02.jpg DSC_0059_03.jpg DSC_0059_04.jpg... (5 Replies)
Discussion started by: jowens1138
5 Replies

4. Solaris

Replacing a string in a long list of files

I have a script that needs to read a file with a long list of /path/filenames - replace the name of the server in each file - and write the file to the same path with a date extension. This is the script that I have so far #!/bin/ksh umask 022 LIST=`scripts.list` for i in $LIST do ... (2 Replies)
Discussion started by: bjdamon
2 Replies

5. Solaris

calculate sum size of files by date (arg list too long)

Hi, I wanted a script to find sum of files for a particular date, below is my script ls -lrt *.req | nawk '$6 == "Aug"' | nawk '$7 == "1"'| awk '{sum = sum + $5} END {print sum}' However, i get the error below /usr/bin/ls: arg list too long How do i fix that. Many thanks before. (2 Replies)
Discussion started by: beginningDBA
2 Replies

6. Shell Programming and Scripting

TAR Files Argument list too long error

Hi, I have a requirement where I need to TAR more than 50K files. Even though I can do TAR successfully on few 100s of files, but whenever Im trying to TAR the entire 50K files, I am getting the error message : Argument List Too Long. Please suggest how can i avoid this error. Im... (2 Replies)
Discussion started by: unx100
2 Replies

7. Shell Programming and Scripting

find list of files from a list and copy to a directory

I will be very grateful if someone can help me with bash shell script that does the following: I have a list of filenames: A01_155716 A05_155780 A07_155812 A09_155844 A11_155876 that are kept in different sub directories within my current directory. I want to find these files and copy... (3 Replies)
Discussion started by: manishabh
3 Replies

8. UNIX for Dummies Questions & Answers

Find files and display only directory list containing those files

I have a directory (and many sub dirs beneath) on AIX system, containing thousands of file. I'm looking to get a list of all directory containing "*.pdf" file. I know basic syntax of find command, but it gives me list of all pdf files, which numbers in thousands. All I need to know is, which... (4 Replies)
Discussion started by: r7p
4 Replies

9. UNIX for Dummies Questions & Answers

arg list too long when trying to tar files

Hi, I am trying to perform this task: tar -cvf tar.newfile ??????.bas I got error "arg list too long". Is ther any way around? I have about 1500 file need to be tar together. Thanks in advance (5 Replies)
Discussion started by: jds3
5 Replies

10. UNIX for Dummies Questions & Answers

arg list too long when mv files?

hello all i need some help because i am a unix/linux dummy...i have the following: DIR1> has 121437 files in it with varying dates going back to early April, a sub dir DIR1/DIR2> has 55835 files in it I need to move all files (T*.*) out of DIR1 into DIR2 that are older than today? Ive been... (2 Replies)
Discussion started by: jamos007
2 Replies
Login or Register to Ask a Question