Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beyond Finding Stuff with the Linux find Command
1
Beyond Finding Stuff with the Linux find Command

[Image: beyond-finding-stuff-with-the-linux-find-command.jpg]

[Image: beyond-finding-stuff-with-the-linux-find-command.jpg]

Continuing the quest to become a command-line power user, in this installment, we will be taking on the
Code:
find
command.

Jack Wallen already covered the basics of
Code:
find
in an article published recently here on Linux.com. If you are completely unfamiliar with
Code:
find
, please read that article first to come to grips with the essentials.

Done? Good. Now, you need to know that
Code:
find
can be used to do much more than just for search for something, in fact you can use it to search for two or three things. For example:

Code:
find path/to/some/directory/ -type f -iname '*.svg' -o -iname '*.pdf'

This will cough up all the files with the extensions
Code:
svg
(or
Code:
SVG
) and
Code:
pdf
(or
Code:
PDF
) in the path/to/directory directory. You can add more things to search for using the
Code:
-o
over and over.

You can also search in more than one directory simultaneously just be adding them to the route bit of the command. Say you want to see what is eating up all the space on your hard drive:

Code:
find $HOME /var /etc -size +500M

This will return all the files bigger than 500 Megabytes (
Code:
-size +500M
) in your home directory, /var and /etc.

Additionally,
Code:
find
also lets you do stuff with the files it… er… finds. For example, you can use the
Code:
-delete
action to remove everything that comes up in a search. Now, be careful with this one. If you run

Code:
# WARNING: DO NOT TRY THIS AT $HOME find . -iname "*" -delete

Code:
find
will erase everything in the current directory (
Code:
.
is shorthand for “the current directory“) and everything in the subdirectories under it, and then the subdirectories themselves, and then there will be nothing but emptiness and an unbearable feeling that something has gone terribly wrong.

Please do not put it to the test.

Instead, let’s look at some more constructive examples…

Moving Stuff Around

Let’s say you have bunch of pictures of Tux the penguin in several formats and spread out over dozens of directories, all under your Documents/ folder. You want to bring them all together into one directory (Tux/) to create a gallery you can revel in:

Code:
find $HOME/Documents/ \( -iname "*tux*png" -o -iname "*tux*jpg" -o -iname "*tux*svg" \)   -exec cp -v '{}' $HOME/Tux/ \;

Let’s break this down:

Once you have the basics of modifying files using
Code:
find
under your belt, you will discover all sorts of situations where it comes in handy. For example…

A Terrible Mish-Mash

Client X has sent you a zip file with important documents and images for the new website you are working on for them. You copy the zip into your ClientX folder (which already contains dozens of files and directories) and uncompress it with
Code:
unzip newwebmedia.zip
and, gosh darn it, the person who made the zip file didn’t compress the directory itself, but the contents in the directory. Now all the images, text files and subdirectories from the zip are all mixed up with the original contents of you folder, that contains more images, text files, and subdirectories.

You could try and remember what the original files were and then move or delete the ones that came from the zip archive. But with dozens of entries of all kinds, you are bound to get mixed up at some point and forget to move a file, or, worse, delete one of your original files.

Looking at the files’ dates (
Code:
ls -la *
) won’t help either: the Zip program keeps the dates the files were originally created, not when they were zipped or unzipped. This means a “new” file from the zip could very well have a date prior to some of the files that were already in the folder when you did the unzipping.

You probably can guess what comes next:
Code:
find
to the rescue! Move into the directory (
Code:
cd path/to/ClientX
), make a new directory where you want the new stuff to go (
Code:
mkdir NewStuff
), and then try this:

Code:
find . -cnewer newwebmedia.zip -exec mv '{}' NewStuff \;

Breaking that down:
  • The period (
    Code:
    .
    ) tells
    Code:
    find
    to do its thing in the current directory.

  • Code:
    -cnewer
    tells
    Code:
    find
    to look for files that have been changed at the same time or after a certain file you give as reference. In this case the reference file is
    Code:
    newwebmedia.zip
    . If you copied the file over at 12:00 and then unpacked it at 12:01, all the files that you unpacked will be tagged as changed at 12:01, that is, after
    Code:
    newwebmedia.zip
    and will match that criteria! And, as long as you didn’t change anything else, they will be the only files meeting that criteria.

  • The
    Code:
    -exec
    part of the instruction simply tells
    Code:
    find
    to move the files and directories to the
    Code:
    NewStuff/
    directory, thus cleaning up the mess.

If you are unsure of anything
Code:
find
may do, you can swap
Code:
-exec
for
Code:
-ok
. The
Code:
-ok
option forces
Code:
find
to check with you before it runs the command you have given it. Accept an action by typing y or reject it with n.

Next Time

We’ll be looking at environmental variables and a way to search even more deeply into files with the
Code:
grep
command.
Reply


Messages In This Thread
Beyond Finding Stuff with the Linux find Command - by xSicKxBot - 11-13-2018, 05:30 PM


Forum Jump:


Users browsing this thread: