Home Download Ubuntu

How to find files from the command line in Ubuntu server

The find command searches files within a directory and its subdirectories according to several criteria such as name, size and time of last read/write. By default find prints the name of the located files but it can also perform commands on these files.

1. Find a file by knowing it's name:
find $HOME -name 'mysong.ogg'

where $HOME is your home directory; mysong.ogg is the file we are looking for;

2. Find a file by knowing a small part of it's name:
find /path/to/your/home -name '*cat*'

where /path/to/your/home is the path where you're searching; -name finds a file by a part of it's name, in this case "cat";

3. Find a file of a specific filetype:
find $HOME -iname '*.ogg'

find all ogg files in your home directory. '-iname' performs a case-insensitive search while the wildcard character '*' matches any character, or number of characters, or zero characters

4. Locating a file by it's size:
find $HOME -iname '*.ogg' -size +100M

here we are searching for all ogg files that are 100 megabytes or larger;

5. Locating files by acces time:
find $HOME -atime +30

here we are searching for all files that have an access time of greater than 30 days;

6. Performing actions on files:
sudo find / -name '*cat*' -exec rm {} \;

here we are searching for all files in the root, that contains the cat string and deletes them. use this command very cautiously.

For more info and all the options:

man find


Resources: ubuntu help documentation, ubuntu forums

0 comments:

Post a Comment