Linux provides a number of powerful command line tools for working with files. This article shows you how to search for files, directories and so on and how to perform custom actions on them.
1. Searching Files
Linux comes with a tool named find. It is available at command line and it is easy to use. The following subsections provide details regarding the usage of find and a number of examples.
find accepts a lage set of (optional) command line parameters. The first (optional) parameter defines the directory name, where find should start working. If not specified the current directory './' will used.
Please note that nearly all query types may be concatenated and combined at a single command line. Details regarding this powerful feature can be found at the end of the article.
1.1. Finding Files by Name
# list files and directories by traversing directory trees
$ find
.
./dir1
./dir1/file1
./dir1/subdir2
./dir1/subdir2/howto
./dir1/subdir2/readme
./dir1/subdir2/README
./dir1/subdir1
./dir1/subdir1/linux-support
./dir1/subdir1/license
# find by name (case sensitive)
$ find -name "howto"
./dir1/subdir2/howto
# find by name (case insensitive)
find -iname "readme"
./dir1/subdir2/readme
./dir1/subdir2/README
# find by name by utilizing wildwards (case sensitive)
$ find -name "*dir?"
./dir1
./dir1/subdir2
./dir1/subdir1
# find by name by utilizing wildwards (case insensitive)
$ find -iname "*ea*"
./dir1/subdir2/readme
./dir1/subdir2/README
1.2. Finding by Regex
For sure there is no doubt, that find is supporting regular expressions. There is even the possibility to choose from a list of supported dialects!
Please note that the regular expression has to match the whole path!
# find by regex (case sensitive)
$ find -regex '.*/r.*'
./dir1/subdir2/readme
# find by regex (case insensitive)
$ find -iregex '.*/r.*'
./dir1/subdir2/readme
./dir1/subdir2/README
1.3. Finding Files by Type
The following list of examples show you how to search for several types of file system objects.
The following list of type identifiers is available:
- b: block (buffered) special
- c: character (unbuffered) special
- d: directory
- p: named pipe (FIFO)
- f: regular file
- l: symbolic link
- s: socket
# search for files
$ find -type f
./dir1/file1
./dir1/subdir2/howto
./dir1/subdir2/readme
./dir1/subdir2/README
./dir1/subdir1/linux-support
./dir1/subdir1/license
# search for directories
$ find -type d
.
./dir1
./dir1/subdir2
./dir1/subdir1
1.4. Find by Permissions
find provides features to search for files depending on their permissions. Please note that there are a number of more sophisticated search parameters available.
# search for files that are readable by the current user
$ find -readable
.
./dir1
./dir1/file1
./dir1/subdir2
./dir1/subdir2/howto
./dir1/subdir2/readme
./dir1/subdir2/README
./dir1/subdir1
./dir1/subdir1/linux-support
./dir1/subdir1/license
# search for files that are writable by the current user
$ find -writable
.
./dir1
./dir1/file1
./dir1/subdir2
./dir1/subdir2/howto
./dir1/subdir2/readme
./dir1/subdir2/README
./dir1/subdir1
./dir1/subdir1/linux-support
./dir1/subdir1/license
# search for files that are executable by the current user
$ find -executable
.
./dir1
./dir1/subdir2
./dir1/subdir1
1.5. Finding by Ownership
The following commands can the utilized to find files owned by groups and users.
# search by user name
$ find -user nina
.
./dir1
./dir1/file1
./dir1/subdir2
./dir1/subdir2/howto
./dir1/subdir2/readme
./dir1/subdir2/README
./dir1/subdir1
./dir1/subdir1/linux-support
./dir1/subdir1/license
# if you do know the user id yon may perform the same search by
# defining the associated integer value
$ find -uid 724
[list of files]
# search by group name
$ find -group designes
.
./dir1
./dir1/subdir1
./dir1/subdir1/linux-support
./dir1/subdir1/license
# if you do know the group id yon may perform the same search by
# defining the associated integer value
$ find -gid 187
[list of files]
1.6. Deleting Files or Directories
If you do want to delete certain files you can assemble a nice query by utilizing find and you just have to append the parameter -delete or -prune to command line to delete files or directories.
# delete all files
$ find /path/to/log_dir -type f -delete
# delete all directories
$ find /path/to/log_dir -type d -prune
1.7. Custom Output
find provides a number of commands to generate custom outputs. This may be usefull if you are intereted in details the command ls is nor able to provide or if you want to prepare a list of statements to be processed or executed at a later point of time.
To get an idea what arguments are supported by -printf you should take a look into the man page of find.
# utilize '-printf' that provides a lage list of parameters to
# assemble custom strings
$ find -type f -printf "cp %p newtarget/%f_001\n"
cp ./dir1/file1/file1 newtarget/file1_001
cp ./dir1/subdir2/howto newtarget/howto_001
cp ./dir1/subdir2/readme newtarget/readme_001
cp ./dir1/subdir2/README newtarget/README_001
cp ./dir1/subdir1/linux-support newtarget/linux-support_001
cp ./dir1/subdir1/license newtarget/license_001
# '-fprintf' take the same list of arguments but writes its
# outputs to a named file
$ find -type f -fprintf mylist.csv "path:%p;file:%f\n"
Custom Actions
When you did assemble a query that fits your needs you may run a custom command, script or executable to process query results line-by-line. The string '{}' will be replaced by the search result and your command has to be closed by '\;'.
# set the timestamp of all files to 'now'
$ find -type f -exec touch {} \;
# set the timestamp of all directories to 'now'
$ find -type d -exec touch {} \;
# print the contents of all files starting with the substring "read"
# in their name to standard out
$ find -type f -name "read*" -exec cat {} \;
[...]
1.8. Conclusion
find is a very powerful command espcially in combination with other tools like xargs, grep, awk or custom scripts. This article contains just a subset of possible commands that are simple to use. If you are interested in advanced examples or the full set of supported parameters you should take a look into the man page of find.




