how to remove files with only numbers as file names?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to remove files with only numbers as file names?
# 1  
Old 06-06-2007
Error 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  
Old 06-06-2007
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  
Old 06-06-2007
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  
Old 06-06-2007
Code:
ls [0-9]* | awk '/^[0-9]+$/{print}' |xargs rm

# 5  
Old 06-07-2007
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  
Old 06-07-2007
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 10:19 AM.. Reason: Use basename.
# 7  
Old 06-07-2007
that was fast!thank you

That was really fast! You rock man!

Thanks,
Praveen
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove all files with specific file names in directory

If I have 5 files in a directory, what is the best way to remove specific files in it? For example, snps.ivg probes.ivg Desired output probes.ivg probes.txt all.txt Basically, removing those files with "snp" in the filename regardless of extension. Thank you :). (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

3. Shell Programming and Scripting

Using awk to append incremental numbers to the end of duplicate file names.

I'm currently working on a script that extracts files from a .zip, runs an sha1sum against them and then uses awk to pre-format them into zomething more readable thusly: Z 69 89e013b0d8aa2f9a79fcec4f2d71c6a469222c07 File1 Z 69 6c3aea28ce22b495e68e022a1578204a9de908ed File2 Z 69... (5 Replies)
Discussion started by: Ethereal
5 Replies

4. Shell Programming and Scripting

Print numbers along with file names.

Hi All, I have some thousand files with names like 1.syl, 2.syl, 5.syl etc. These files contain one sentence each. I want to store all those sentences along with the file ID that is 1, 2, 5 with the sentences they contain. For example, 1.syl has this is a test line 2.syl has ... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

5. Shell Programming and Scripting

How to remove common file names from text files

I'm running on freebsd -- with a default shell of csh. I have two files named A and B. Each line of each file contains a file name. How can I write a script that removes all the file names in file B from A. I tried to use perl to create a huge regular expression with "|" separating the file... (2 Replies)
Discussion started by: siegfried
2 Replies

6. Shell Programming and Scripting

Shell Scripts (Renaming file names with sequential numbers)

Hi there, Firstly, I have no experience with shell scripts so would really appreciate some help. I have the following shell script that is causing some problems: moveit() { && set -x if then DOUBLE_DELIVERY=$(grep... (6 Replies)
Discussion started by: thebeno
6 Replies

7. Shell Programming and Scripting

Finding consecutive numbers in version names on a txt file

Hi all. I have a directory which contains files that can be versioned. All the files are named according to a pattern like this: TEXTSTRING1-001.EXTENSION TEXTSTRING2-001.EXTENSION TEXTSTRING3-001.EXTENSION ... TEXTSTRINGn-001.EXTENSION If a file is versioned, a file called ... (10 Replies)
Discussion started by: fox1212
10 Replies

8. UNIX for Advanced & Expert Users

How to select only those file names whose name contains only numbers

Hi Guru's, Before writing to this forum I have searched extensively on this forum about my problem. I have to write a shell script which takes out only those file names from the given directory which contains only numbers. For example, In the given directory these files are present: ... (4 Replies)
Discussion started by: spranm
4 Replies

9. UNIX for Dummies Questions & Answers

How to select only those file names whose name contains only numbers.

Hi Guru's, Before writing to this forum I have searched extensively on this forum about my problem. I have to write a shell script which takes out only those file names from the given directory which contains only numbers. For example, In the given directory these files are present: ... (0 Replies)
Discussion started by: spranm
0 Replies

10. Shell Programming and Scripting

Remove spaces between file names

Hi All, I have spaces in between file names. "Material Header.txt" "Customer Header.txt" "Vendor Header.txt" And how can I remove spaces between file names like below MaterialHeader.txt CustomerHeader.txt VendorHeader.txt Thanks Srimitta (10 Replies)
Discussion started by: srimitta
10 Replies
Login or Register to Ask a Question