Closed Captioning Closed captioning available on our YouTube channel

Linux tip: How to use the find command

Network World | Jul 10, 2018

In today’s Linux tip, we look at the find command – a tool that will prove very useful when you’re trying to locate a file or set of files based on almost any criteria.

Copyright © 2018 IDG Communications, Inc.

Similar
Hi, this is Sandra Henry-Stocker, author of the “Unix as a Second Language” blog on NetworkWorld.
In today’s Linux tip, we’re going to look at the find command – a tool that will prove very useful when you’re trying to locate a file or set of files based on almost any criteria – maybe the file’s size, name, owner, permissions, when it was last accessed or updated, when its status last changed, whether it’s executable or empty, its inode number and more. In fact, the number of options available for the find command is enough to make you dizzy.
So, let’s take a look at how it works.
Given only a file name, find looks in the current directory only. Here we’re asking it to find a file named “tryme”.
$ find tryme
If we add our criteria, it looks in the current directory and subdirectories:
$ find -name tryme
We can search for files by size almost as easily:
$ sudo find /home -size +10000
Here, I’m using sudo because my user account probably doesn’t allow me to look into other users’ home directories. Notice that I can start my search in the home directory regardless of where I’m sitting in the file system right now, and here I’m asking to locate all files larger than 10,000 bytes.
If I run the same command with -ls on the end, I also get to see inode numbers, file sizes in blocks and bytes, and file permissions – along with the files’ full locations.
$ sudo find /home -size +10000 -ls
Now let’s try some find commands that are a bit more exotic. With this next command, I am looking for files with particular permissions – the owner and group must both have write permission. Other permissions are irrelevant.
$ sudo find /home/nemo -perm /u=w,g=w
With this next command, I’m looking for files that are empty. If I didn’t specify that the type was “f” (file), I’d also find empty directories.
$ find . -empty -type f -ls
Now let’s count how many files have been accessed in the last hour in /home.
$ sudo find /home -amin -60 | wc -l
In the next command, I’ll find files in and below the current directory that don’t belong to me. The ! sign represents “NOT”.
$ find . ! -user shs -ls
$ find . ! -user $USER -ls
Note that the second command would work for anyone because I’ve used $USER in place of my username.
This next command will find files in my home directory that don’t belong to me and ask me if I want to remove them.
$ find $HOME ! -user $USER -ok rm {} \;
Read this as “find in my home directory files that DON’T belong to current user (me) and remove them”. The {} brackets are a place holder for the file names.
This last command tells find to locate files that have no currently active owner.
$ find . -nouser -ls
If we add an exec clause, we can move these files to a temporary directory:
$ mkdir /tmp/$USER
$ find . -nouser -exec mv {} /tmp/$USER \;
Notice how we have to end the -exec clause in this command with a backslash and a semicolon. You can use the -exec clause to run almost any command involving the file – mv, rm, chown, head and tail come to mind.
As I said up front, there are a LOT of options for finding specific files using the find command and manipulating them in some way.
That’s your Linux tip for today. If you liked this video, please hit the like and share buttons. For more Linux tips, be sure to follow us on Facebook, YouTube and NetworkWorld.com.
Popular
Featured videos from IDG.tv