Terminal commands
Search by Type (file or dir)
find .| Find all the files & directories under current directoryfind <dir>| Find all the files & directories underfind <dir> -type d| Find only directories under specific directoryfind . -type d| Find only directories under current directoryfind <dir> -type f| Find only files under specific directoryfind . -type f| Find only files under current directory
Search by file name
find <dir> -type f -name "<test1.txt>"| Find a file with a specific namefind <dir> -type f -name "<test*>"| Find a file starting with the name ‘test’find <dir> -type f -iname "<test*>"| Find a file starting with the name ‘test’ or ‘Test’ & Case Insensitivefind <dir> -type f -iname "<*.py>"| Find a file ending with the name ‘.py’ & Case Insensitive
Search by time / day
find <dir> -type f -mmin -10| All the files modified in last 10 minsfind <dir> -type f -mmin +10| All the files modified in more than last 10 mins agofind <dir> -type f -mmin +1 -mmin -5| More than 1 min ago and less than 5 mins agofind <dir> -type f -mtime -20| Files modified less than 20 days ago- mmin, mtime
- amin, atime
- cmin, ctime
Search by file size
find <dir> -size +5M| Find all the files over 5 MB underdirectory
- M - MB
- k - KB
- G - GB
ls -lah <dir>| List all files and dirs with size in MBfind <dir> -empty| Find all empty files indirectory
Search based on permissions
find <dir> -perm 777
-exec| Executes the following command on the results from preceding command{}| Placeholder for just the filenamees that would be be used in case of chown command+or\;| Either can be used to end the commandfind <dir> -exec chown [user]:[group] {} +
Lets try setting permissions of all directories to 775 and all files to 664
find <dir> -type d -exec chmod 775 {} +find <dir> -type f -exec chmod 664 {} +
Search and perform actions
find <dir> -type f -name "*.jpg"| Search all image files ending in .jpg inand subsequent directories find <dir> -type f -name "*.jpg" -maxdepth 1| Search all image files ending in .jpg only and only infind <dir> -type f -name "*.jpg" -exec rm {} +| Deletes all the files returned from the command precesing-execfind <dir> -type f -name "*.jpg" -maxdepth 1 -exec rm {} +| Deletes all the files returned from the command precesing-exec
_grep (Global Regular Expression Print)
grepis case sensitive
Finding is a given text is present in some file
grep "text_to_find" <file_name>| Searching for some text in a normal filegrep -w "text_to_find" <file_name>| Return results fromfile_nameonly when whole words matchtext_to_findgrep -wi "text_to_find" <file_name>| Returns results with both lower case and upper case withtext_to_find
Finding some additional information
Line number of where we found our match
- Returns results with line number.
grep -win "text_to_find" <file_name>| Returns results with line numbers infile_namegrep -win "text_to_find" ./*| Returns results with line numbers in all files in current directory + Will throw error for any subdirectory that might be present.grep -win "text_to_find" ./*.txt| Doesn’t try to search in any subdirectory.grep -winr "text_to_find" .| To search every file and through every subdirectory, a recursive search, might get lot of results.
- Getting some additional context of where this match is found, see a certain number of lines before and after a match.
grep -win -B 4 "text_to_find" <file_name>| 4 lines Before all of our matchgrep -win -A 4 "text_to_find" <file_name>| 4 lines After all of our matchgrep -win -C 2 "text_to_find" <file_name>| 2 lines Before and After all of our match
- If you’re only interested in file_names with the matches, NOT in the matches themselves.
grep -wirl "text_to_find" .| Recursive result of all files with the matchgrep -wirc "text_to_find" .| Recursive result of all files with the match + Number of matches in each file
Pipe the output of other commands in to grep to search for something
history | grep "git commit"history | grep "git commit" | grep "dotfile"
grep uses Posix regular expressions by default
grep "...-...-...." <filename>grep "\d{3}-\d{3}-\d{4}" <filename>| This wouldn’t work, because this is pro compatible regular expressions which grep doesn’t use.grep -P "\d{3}-\d{3}-\d{4}" <filename>| This would allow it to work on Linux, NOT on Macgrep -wirlP "\d{3}-\d{3}-\d{4}" <filename>| Return recursive list of list with matching phone numbersgrep -V- Mac uses BSD grep
- Linux uses GNU grep`
brew install grep --with-default-names--with-default-names| It will install it as grep, else as ggrep (allowing us to use both BSD and GNU grep)