LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-01-2010, 08:49 PM   #1
th1bill
Member
 
Registered: Dec 2007
Location: SE Texaas
Distribution: Ubuntu, Mint, Fedora
Posts: 168

Rep: Reputation: 24
string f file to remove


I have a string of files named recup_dir.1 thru .66. I have the command sudo rm -r /home/"name"/Pictures/recup_dir.1 to remove them one at a time, how do I remove 20 or thirty at one command?

I hope you have mercy on a former Windoze mechanic.
 
Old 01-01-2010, 09:36 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
There are two choices.

The first uses the range brackets found in regular expressions and globbing.
Code:
rm file.[1-10]
rm file.[1-9]

# Looks in the directory for file.1 file.2 file.3 ... file.10 file.9
# then runs "rm <matching files>"

rm file.[1-9] file.[1-2][0-9]
# runs on file.1 to file.9, and then file.10 to file.29
This looks first to see what files match the range given, then substitutes their filenames in the final command. rm only gets a list of whatever files exist in that sequence. If no files exist, it will treat it as a literal "file.[1-10]" "file.[1-9] (which will likely not match anything). This is probably your safest choice here.

[ Edit the next day: Sorry, messed up yesterday. The regex range expression will only match single digits/characters. You have to get a bit more creative when matching multi-digit numbers, particularly when trying to match both single and multi-digit numbers at the same time. ]


The second option is to use brace expansion. The braces generate a series of text strings, one for each step in the range.

Code:
rm file.{1..10}

# Equivalent to running "rm file.1 file.2 file.3 ... file.10"
In this case, rm is simply given a list of 10 files to attempt to remove. Since it generates one command for each number, separate errors will occur for each file not found.

Brace expansion is usually used more for commands that create series' of files instead.

Code:
touch file.{1..10}  #creates a series of 10 empty text files
Note that bash version 4 has introduced zero-padding and incrementing by steps in brace expansion.

Last edited by David the H.; 01-02-2010 at 10:45 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell scripting - Getting just directory string from file path string? arashi256 Programming 5 10-16-2009 08:21 AM
read string after specific string from a text file using C++ programing language badwl24 Programming 5 10-08-2009 05:41 AM
Remove last character from file/string linuxchump Programming 34 06-08-2009 04:01 AM
How to remove string in the text file ? Bash script dlugasx Linux - Server 9 06-05-2009 11:40 AM
How to remove spaces from a string pawan_songara Programming 14 08-30-2006 09:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration