Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Which command returns all files that end with a single digit and have the TXT extension ?

share|improve this question
    
Is the "single digit" part important? My solution allows for more than one as long as the final character is a digit. If that's important, please edit your question to be more explicit and we'll adapt. –  Caleb Nov 13 '12 at 21:03
    
The Unix command should return the files that end single digit as well as as .txt extension viz. 0.txt,1.txt,3.txt..... and the command to be used is ls Ans: ls -l_________________ –  Sai Nov 17 '12 at 11:01
    
I'm sorry Sai but that didn't actually clarify what your issue is. Would you like it to match filename34.txt or just filename4.txt (single digit) or only file names that are a single digit like 4.txt? Please use the edit function to make your question more specific so we can adapt our answers. Right now everybody is guessing what you want. Some effort from you to explain the situation will help you get better answers. –  Caleb Nov 17 '12 at 17:53
add comment

4 Answers

One of the most basic tools for locating files (or other kinds of nodes) is the find utility.

find ./ -type f -name '*[!0-9][0-9].txt'

This will search:

  • ...recursivly from the current directory (./). You could change this to another path or even leave it off since this is the default value for most versions of find.
  • ...for items that are files as opposed to directories, devices nodes, symbolic links, etc (-type f). You could leave this off if you wanted to find other types too.
  • ...for items that match the name pattern given. Note that I have used single quotes to encose the pattern so that bash or your shell does not try to expland it to a glob pattern before find even gets the command. The star matches any number of characters, then the end of the file must be something other than a digit, then a digit, then your extention. (-name '*[0-9].txt')

If there are files whose name is only a digit followed by .txt, the command above will miss them, because it requires one non-digit before the digit. The following equivalent commands use boolean operators to include file names with just a digit as well (-o is “or” and ! is “not”):

find ./ -type f \( -name '*[!0-9][0-9].txt' -o -name '[0-9].txt' \)
find ./ -type f -name '*[0-9].txt' ! -name '*[0-9][0-9].txt'

Note that this will be case sensitive. If you would like an insensitive match, you can use -iname instead of -name to match things like file4.TXT as well. Also be aware that just because a file claims to be a text file with that extention does not mean it is. On linux any file can be any type no matter the name. There might also be text files with other extentions or with no extention at all.

share|improve this answer
    
Note if bash is your shell then you have to escape the ! too (disabling some history features might also suffice). –  dubiousjim Nov 14 '12 at 2:18
add comment

Using plain old POSIX globs:

ls -d -- [0-9].txt *[!0-9][0-9].txt
share|improve this answer
3  
Since the OP asked for a single digit, something like *[^0-9][0-9].txt might be a better glob (although that would miss filenames consisting of only a single digit plus .txt. –  Ansgar Esztermann Nov 13 '12 at 13:11
    
Good point @AnsgarEsztermann. –  dubiousjim Nov 13 '12 at 13:41
    
@StephaneChazelas: thanks for the -d --, I thought of suggesting these only after posting. As an alternative, as ChrisDown proposes in his answer, one could use printf '%s\n' YourGlobPattern instead of ls .... –  dubiousjim Nov 13 '12 at 13:41
add comment

In zsh, with setopt extended_glob in ~/.zshrc:

print -lr -- *[0-9].txt~*[0-9][0-9].txt

If your file names don't begin with - and don't contain \ you can save a bit of typing:

print -l *[0-9].txt~*[0-9][0-9].txt

Replace print -l by echo to show the names with spaces between them instead of newlines. Replace print -l by ls -d to see the file names formatted by ls (e.g. in color, depending on your aliases), or by just ls if none of the matches are directories:

ls *[0-9].txt~*[0-9][0-9].txt

If you want to see files in subdirectories as well, add **/ before the pattern:

ls **/*[0-9].txt~*[0-9][0-9].txt
share|improve this answer
add comment

The Unix command to return the files that ends with single digit and has .txt extension ls -l *[0-9].txt

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.