The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check file size and remove files srivsn Shell Programming and Scripting 5 05-28-2009 01:09 PM
shell script to remove old files and write to a log file yabai Shell Programming and Scripting 4 12-09-2008 01:08 PM
total number of files which have "aaa" in files whose names are File*_bbb* sudheshnaiyer UNIX for Dummies Questions & Answers 1 08-16-2007 02:34 PM
How to remove all lines with something other than numbers Juha Shell Programming and Scripting 2 09-21-2006 04:48 PM
How can I delete files using a file that containt path and names? AlvaroD UNIX for Dummies Questions & Answers 2 09-25-2001 12:51 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-06-2007
praveen_indramo praveen_indramo is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 10
Exclamation how to remove files with only numbers as file names?

Hi all,

I have a bunch of files that are named like 12543, 467249877, etc all over some directories.These files are named only with numbers, they dont have any letters or special characters in their file names. Could you please help me out and give me some command/script to remove only those files?
I really appreciate it.

Thanks,
Praveen
  #2 (permalink)  
Old 06-06-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Praveen,
See if this works for you:
Code:
for mFName in `ls -1 [0-9]*`
do
  mNoNbrs=`echo $mFName | sed 's/[0-9]//g'`
  if [ "${mNoNbrs}" = "" ]; then
    echo "ONLY NUMBERS"
  fi
done
  #3 (permalink)  
Old 06-06-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,420
With ksh and bash (option extglob set) you can list and remove the files with the command :
Code:
$ ls +([0-9])
$ rm +([0-9])
Jean-Pierre.
  #4 (permalink)  
Old 06-06-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,527
Code:
ls [0-9]* | awk '/^[0-9]+$/{print}' |xargs rm
  #5 (permalink)  
Old 06-07-2007
praveen_indramo praveen_indramo is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 10
Thanks a lot guys!But..

guys,

thats really awesome..I got so many responses so fast.But I need to do this recursively in directories and subdirectories.. so i was hoping to use a find command..like find . -name blah* . Please let me know if you have anything handy to find/remove these files. For example, i have files like ~/12345 and ~/sub/3256434, ~/sub2/sun3/98345, etc.

Praveen
  #6 (permalink)  
Old 06-07-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Praveen,
As you can see, there are several ways to skin a cat.
Using my solution:
Code:
for mFName in `find . -type f -name '[0-9]*'`
do
  mNoNbrs=`basename $mFName | sed 's/[0-9]//g'`
  if [ "${mNoNbrs}" = "" ]; then
    echo "ONLY NUMBERS"
  fi
done

Last edited by Shell_Life; 06-07-2007 at 09:19 AM.. Reason: Use basename.
  #7 (permalink)  
Old 06-07-2007
praveen_indramo praveen_indramo is offline
Registered User
  
 

Join Date: Sep 2006
Posts: 10
that was fast!thank you

That was really fast! You rock man!

Thanks,
Praveen
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0