22dec11
DEBIAN, CRUNCHBANG, LINUX, COMMAND LINE







Find files, folders and text in Linux from command line

Just snitched snippets from other how-tos. Sources at the end.



Find
Find is the more complex and elaborate command compared to locate. It is mostly installed in any Linux distribution.
Use:
find directory file/folder
examples:
find -name docu*
find /home/tintin -name 'snowy*'
ind /home/tintin -iname 'snowy*'
find /music -name '*.mp3' -size -5000k
find / -size +10000k

("index" at the beginning")
(same as before - only: InDeX, indEX is also a hit)
You can search for text in files too:
find <directory to search> -type f | xargs grep -rl '<text to search for>'
e.g:
find /home/tintin/files/ -type f | xargs grep -rl 'snowy'
Or:
find <directory to search> -type f -exec grep -l ‘<text to search for>’ {} +
e.g:
find /home/tintin/files/ -type f -exec grep -l 'snowy' {} +


Locate
Install:
sudo apt-get install locate
Use:
sudo updatedb
locate file/folder
Examples:
locate index.php
locate homepage


Grep
Also an elaborate search tool. (See cut too!)
Use:
grep -rl ‘<text to search for>’ <directory to search>/*
e.g:
grep -rl 'snowy' /home/tintin/files/*
Other examples:
grep 'snowy' notes.txt
grep -i 'snowy' notes.txt
grep -ic 'snowy' notes.txt
grep -i -v 'snowy' notes.txt
grep '^snowy' notes.txt
grep 'snowy$' notes.txt
There is much more!
(print all lines found in notes.txt with snowy in it)
(print all lines with snowy in it, ignoring upper-, lowercase)
(print number of lines with snowy in it)
(print all lines with snowy NOT in it)
(print all lines with snowy at the beginning)
(print all lines with snowy at the end)

Find and replace text in documents:
grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'


Cat
The cat command is not actually used to search for anything but to print the contents of text files. (See tail too!)
Use:
cat /home/tintin/files/notes







Sources:
http://news.softpedia.com/news/Finding-Files-and-Directories-from-the-Command-Line-36654.shtml

http://www.my-guides.net/en/guides/linux/245-how-to-search-for-text-in-files-using-linux-command-line
http://systembash.com/content/easy-search-and-replace-in-multiple-files-on-linux-command-line/
http://lowfatlinux.com/linux-grep.html