Last active
December 3, 2015 06:07
-
-
Save vparihar01/38464405d71cae4a4972 to your computer and use it in GitHub Desktop.
Using Wildcards in bash for various file related task like list, rm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# How To Use Find and Locate to Search for Files on a Linux | |
$ find -name "name_of_file_or_dir" #Finding by Name | |
$ find -iname "name_of_file_or_dir" #To find a file by name, but ignore the case of the name_of_file_or_dir | |
# Finding by Type | |
$ find -type type_descriptor query | |
#Some of the most common descriptors that you can use to specify the type of file are here: | |
#f: regular file | |
#d: directory | |
#l: symbolic link | |
#c: character devices | |
#b: block devices | |
$ find / -type f -name "*.conf" #search for all files that end in ".conf" | |
# Filtering by Time and Size | |
#c: bytes | |
#k: Kilobytes | |
#M: Megabytes | |
#G: Gigabytes | |
#b: 512-byte blocks | |
$ find / -size 50c #find all files that are exactly 50 bytes | |
$ find / -size -50c #find all files less than 50 bytes | |
$ find / -size +700M #Find all files more than 700 Megabytes | |
#Time | |
#Linux stores time data about access times, modification times, and change times. | |
#Access Time: Last time a file was read or written to. | |
#Modification Time: Last time the contents of the file were modified. | |
#Change Time: Last time the file's inode meta-data was changed. | |
#We can use these with the "-atime", "-mtime", and "-ctime" parameters | |
$ find / -mtime 1 #find files that have a modification time of a day ago | |
$ find / -atime -1 #files that were accessed in less than a day ago | |
$ find / -ctime +3 #files that last had their meta information changed more than 3 days ago | |
$ find / -mmin -1 #companion parameters we can use to specify minutes instead of days | |
$ find / -newer myfile #files that have been modified type the system in the last minute | |
#Finding by Owner and Permissions | |
#You can also search for files by the file owner or group owner. | |
#You do this by using the "-user" and "-group" parameters respectively | |
$ find / -user syslog #Find a file that is owned by the "syslog" user by entering | |
$ find / -group shadow #Find files owned by the "shadow" group by typing | |
$ find / -perm 644 #match an exact set of permissions, we use this form | |
#Executing and Combining Find Commands | |
#You can execute an arbitrary helper command on everything that find matches by using the "-exec" parameter. | |
$ find find_parameters -exec command_and_params {} \; #The "{}" is used as a placeholder for the files that find matches. The "\;" is used so that find knows where the command ends. | |
# For instance, we could find the files in the previous section that had "644" permissions and modify them to have "664" permissions: | |
$ cd ~/test | |
$ find . -type f -perm 644 -exec chmod 664 {} \; | |
#We could then change the directory permissions like this: | |
$ find . -type d -perm 755 -exec chmod 700 {} \; | |
#If you want to chain different results together, you can use the "-and" or "-or" commands. The "-and" is assumed if omitted. | |
$ find . -name file1 -or -name file9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ls *.jpg # List all JPEG files | |
$ ls ?.jpg # List JPEG files with 1 char names (eg a.jpg, 1.jpg) | |
$ rm [A-Z]*.jpg # Remove JPEG files that start with a capital letter | |
$ shopt -s extglob | |
#Extended globbing as described by the bash man page: | |
# ?(pattern-list) Matches zero or one occurrence of the given patterns | |
# *(pattern-list) Matches zero or more occurrences of the given patterns | |
# +(pattern-list) Matches one or more occurrences of the given patterns | |
# @(pattern-list) Matches one of the given patterns | |
# !(pattern-list) Matches anything except one of the given patterns | |
#pattern-list is a list of items separated by a vertical bar "|" (aka the pipe symbol) | |
#Bash Regular Expression | |
# ?(pattern-list) (...|...)? | |
# *(pattern-list) (...|...)* | |
# +(pattern-list) (...|...)+ | |
# @(pattern-list) (...|...) [@ not a RE syntax] | |
# !(pattern-list) "!" used as for negative assertions in RE syntax | |
# To list all the files that match the regular expression "ab(2|3)+.jpg" you could do: | |
$ ls ab+(2|3).jpg | |
#Now that's something you can't do with regular globbing. | |
#Note: this matches files like ab2.jpg, ab3.jpg, ab2222.jpg, ab333.jpg, etc. | |
#This: | |
ls -alt [a-z]*.{jpg,gif} | |
# lists jpg/gif files that start with any lower case letter followed by any number of other characters. | |
#This: | |
ls -alt [a-z]?.{jpg,gif} | |
#lists jpg/gif files start with any lower case letter followed by any single character. | |
#This: | |
ls -alt t{[0-9]*,[a-z]*}.{jpg,gif} | |
#lists jpg/gif files that start with the letter t followed by either a digit or a lower case letter | |
#and then followed by any number of other characters. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment