Show How to use wild cards to find files beginning with upper and lower case Im trying to use wild cards to find files that start with either an upper or lower case letter e.g. list files that beginning with b or B, i also want to sort them by the time they were last modified. e.g latest file created first. At the moment i have the following code that any suggestions ive tried pipping them as followed ls -d B* | ls -d b* | -lt but had no luck Last edited by vbe; 01-02-2012 at 12:19 PM.. Reason: code tags please next time!
Page 2
How to use wild cards to find files beginning with upper and lower case Im trying to use wild cards to find files that start with either an upper or lower case letter e.g. list files that beginning with b or B, i also want to sort them by the time they were last modified. e.g latest file created first. At the moment i have the following code that any suggestions ive tried pipping them as followed ls -d B* | ls -d b* | -lt but had no luck Last edited by vbe; 01-02-2012 at 12:19 PM.. Reason: code tags please next time!
command that lists all the files (in the current directory) that start with an alphabetic character, contain a digit, and end with a dot followed by a lowercase character then one more character
Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively. Read Also: 10 Useful Practical Examples on Chaining Operators in Linux These wildcards are interpreted by the shell and the results are returned to the command you run. There are three main wildcards in Linux:
You need to carefully choose which wildcard to use to match correct filenames: it is also possible to combine all of them in one operation as explained in the examples below. How to Match Filenames Using Wildcards in LinuxFor the purpose of this article, we will use following files to demonstrate each example. createbackup.sh list.sh lspace.sh speaker.sh listopen.sh lost.sh rename-files.sh topprocs.sh1. This command matches all files with names starting with l (which is the prefix) and ending with one or more occurrences of any character. $ ls -l l* List Files with Character2. This example shows another use of * to copy all filenames prefixed with users-0 and ending with one or more occurrences of any character. $ mkdir -p users-info $ ls users-0* $ mv -v users-0* users-info/ # Option -v flag enables verbose output List and Copy All Files3. The following command matches all files with names beginning with l followed by any single character and ending with st.sh (which is the suffix). $ ls l?st.sh Match File with Character Name4. The command below matches all files with names starting with l followed by any of the characters in the square bracket but ending with st.sh. How to Combine Wildcards to Match Filenames in LinuxYou can combine wildcards to build a complex filename matching criteria as described in the following examples. 5. This command will match all filenames prefixed with any two characters followed by st but ending with one or more occurrence of any character. $ ls $ ls ??st* Match File Names with Prefix6. This example matches filenames starting with any of these characters [clst] and ending with one or more occurrence of any character. $ ls $ ls [clst]* Match Files with Characters7. In this examples, only filenames starting with any of these characters [clst] followed by one of these [io] and then any single character, followed by a t and lastly, one or more occurrence of any character will be listed. $ ls $ ls [clst][io]?t* List Files with Multiple Characters8. Here, filenames prefixed with one or more occurrence of any character, followed by the letters tar and ending with one or more occurrence of any character will be removed. $ ls $ rm *tar* $ ls Remove Files with Character LettersHow to Match Characters Set in Linux9. Now lets look at how to specify a set of characters. Consider the filenames below containing system users information. $ ls users-111.list users-1AA.list users-22A.list users-2aB.txt users-2ba.txt users-111.txt users-1AA.txt users-22A.txt users-2AB.txt users-2bA.txt users-11A.txt users-1AB.list users-2aA.txt users-2ba.list users-12A.txt users-1AB.txt users-2AB.list users-2bA.listThis command will match all files whose name starts with users-i, followed by a number, a lower case letter or number, then a number and ends with one or more occurrences of any character. $ ls users-[0-9][a-z0-9][0-9]*The next command matches filenames beginning with users-i, followed by a number, a lower or upper case letter or number, then a number and ends with one or more occurrences of any character. $ ls users-[0-9][a-zA-Z0-9][0-9]*This command that follows will match all filenames beginning with users-i, followed by a number, a lower or upper case letter or number, then a lower or upper case letter and ends with one or more occurrences of any character. $ ls users-[0-9][a-zA-Z0-9][a-zA-Z]* Match Characters in FilenamesHow to Negate a Set of Characters in Linux10. You can as well negate a set of characters using the ! symbol. The following command lists all filenames starting with users-i, followed by a number, any valid file naming character apart from a number, then a lower or upper case letter and ends with one or more occurrences of any character. $ ls users-[0-9][!0-9][a-zA-Z]*That’s all for now! If you have tried out the above examples, you should now have a good understanding of how wildcards work to match filenames in Linux. You might also like to read these following articles that shows examples of using wildcards in Linux: If you have any thing to share or a question(s) to ask, use the comment form below. |