The find command allows users to do a comprehensive search spanning the directory tree. find also allows the setting of more specific options to filter the search results and when you’ve found what you’re looking for find even has the option to do some work on those files.

Finding Files by Age

What if a user wants to determine if there are any really old files on their server? There are dozens of options for the find command but the first thing find requires is the path in which to look.

In this example we will change our working directory to the / (root) directory and run the find command on the working directory by giving . as the path argument. The following command sequence looks for any files that are more than 20 years, 7300 days, old.

Finding files older than 20 years

# cd /
# find ./ -mtime +7300
./tmp/orbit-root
# cd /tmp
# ls -ld orbit-root
drwx------ 2 root root 8192 Dec 31 1969 orbit-root

By default find prints the name and path to any files which match the criteria listed. In this case it has found a file in ./tmp/orbit-root which has not been modified in more than 7300 days.

You’ve probably noticed that the date on this file is a bit suspect. While the details are unimportant it is worth understanding that anything on a Linux system with a date of December 31, 1969 or January 1, 1970 has probably lost its date and time attributes somehow. It may have also been created at some time when the system’s clock was horribly wrong.

If we wanted to search the root directory without changing our working directory we could have specified the directory in the find command like this:

# find / -mtime +7300
/tmp/orbit-root

The command found the same file in this case but has now described it starting with / instead of ./ because that is what was used in the find command.

The following command sequence will look for some newer files. The process starts in the user’s home directory and looks for files less than three days old.

Finding Any Files Modified in the Past 3 Days

$ cd ~
$ find . -mtime -3.
./.bash_history
./examples
./examples/preamble.txt
./examples/other.txt
./example1.fil
./.viminfo

Now we start to really see the power of the find command. It has identified files not only in the working directory but in a subdirectory as well! Let’s verify the findings with some ls commands:

$ ls –alt
total 56
drwxrwxr-x 2 tclark authors 4096 Feb 3 17:45 examples
-rw------- 1 tclark tclark 8793 Feb 3 14:04 .bash_history
drwx------ 4 tclark tclark 4096 Feb 3 11:17 .
-rw------- 1 tclark tclark 1066 Feb 3 11:17 .viminfo
-rw-rw-r-- 1 tclark tclark 0 Feb 3 09:00 example1.fil
-rw-r--r-- 1 tclark authors 0 Jan 27 00:22 umask_example.fil
drwxr-xr-x 8 root root 4096 Jan 25 22:16 ..
-rw-rw-r-- 1 tclark tclark 0 Jan 13 21:13 example2.xxx
-rw-r--r-- 1 tclark tclark 120 Aug 24 06:44 .gtkrc
-rw-r--r-- 1 tclark tclark 24 Aug 18 11:23 .bash_logout
-rw-r--r-- 1 tclark tclark 191 Aug 18 11:23 .bash_profile
-rw-r--r-- 1 tclark tclark 124 Aug 18 11:23 .bashrc
-rw-r--r-- 1 tclark tclark 237 May 22 2003 .emacs
-rw-r--r-- 1 tclark tclark 220 Nov 27 2002 .zshrc
drwxr-xr-x 3 tclark tclark 4096 Aug 12 2002 .kde
$ cd examples
$ ls -alt
total 20
drwxrwxr-x 2 tclark authors 4096 Feb 3 17:45 .
-rw-rw-r-- 1 tclark tclark 0 Feb 3 17:45 other.txt
-rw-rw-r-- 1 tclark authors 360 Feb 3 17:44 preamble.txt
drwx------ 4 tclark tclark 4096 Feb 3 11:17 ..
-rw-r--r-- 1 tclark authors 2229 Jan 13 21:35 declaration.txt
-rw-rw-r-- 1 tclark presidents 1310 Jan 13 17:48 gettysburg.txt

So we see that find has turned up what we were looking for. Now we will refine our search even further.

Finding .txt Files Modified in the Past 3 Days

Sometimes we are only concerned specific files in the directory. For example, say you wrote a text file sometime in the past couple days and now you can’t remember what you called it or where you put it. Here’s one way you could find that text file without having to go through your entire system:

$ find . -name '*.txt' -mtime -3
./preamble.txt
./other.txt

Now you’ve got even fewer files than in the last search and you could easily identify the one you’re looking for.

Find files by size

If a user is running short of disk space, they may want to find some large files and compress them to recover space. The following will search from the current directory and find all files larger than 10,000KB. The output has been abbreviated.

Finding Files Larger than 10,000k

# find . -size +10000k
./proc/kcore
./var/lib/rpm/Packages
./var/lib/rpm/Filemd5s
...
./home/stage/REPCA/repCA/wireless/USData.xml
./home/stage/REPCA/repCA/wireless/completebootstrap.xml
./home/stage/REPCA/repCA/wireless/bootstrap.xml
./home/bb/bbc1.9e-btf/BBOUT.OLD

Similarly a – could be used in this example to find all files smaller than 10,000KB. Of course there would be quite a few of those on a Linux system.

The find command is quite flexible and accepts numerous options. We have only covered a couple of the options here but if you want to check out more of them take a look at find’s man page.

Most of find’s options can be combined to find files which meet several criteria. To do this we can just continue to list criteria like we did when finding .txt files which had been modified in the past three days.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


Robert Vollman has now posted a review of my book Easy Linux Commands on Amazon.

He makes many good points but one I keep hearing from just about everyone is that almost all of the content of Easy Linux Commands can be applied on other UNIX and UNIX-like systems.

Here is Robert’s full review:

My shelf is full of technical books on a variety of topics, including Linux. But there have been times when someone new to the IT world will ask me for a book to get them started in a particular area. Alas, most of my books are thousand-page, exhaustively-detailed volumes that would be so inaccessible that the only use a beginner could get out of it would be to kill a few spiders.

But now, thanks to Jon Emmons and Terry Clark, I finally have a book I can give a young student, or a previously “Windows-only” PC user. “Easy Linux Commands” is just what it claims to be: an easy introduction to the command-line world.

Being easy to read and accessible is this book’s chief selling point. The book is not only under 200 pages, with lots of pictures, big text and barely 30 lines per page, but it’s also structured in the exact same familiar fashion as countless other books. Furthermore, I don’t find the author’s style overly technical. His writing style is very informal and almost conversational. Judge for yourself by visiting his blog “Life After Coffee,” where he occasionally includes excerpts from the book. In fact, if something is not clear, Jon Emmons is very accessible and answers questions quickly and happily.

http://www.lifeaftercoffee.com/

Also notice that I said this books introduces you to the command-line world, not Linux. I said that for two reasons:
1. Almost everything in this books applies equally well to Unix. Very little in this book is actually Linux-specific.
2. Even though Linux has graphical user interfaces, like Gnome and KDE, this book covers command-line Linux only.

One word of caution. Don’t be thrown by the “Become a Linux Command Guru” picture stamped on the front cover. You won’t be a guru. This covers the basics, and only a little more. But this book will get you past square one and allow you to use some of those big books for becoming a guru (instead of an exterminator).

Easy Linux CommandsCheck out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


linux, book

The ls command is the main way to browse directory contents on UNIX and Linux. While it can be used with no options there are several options which will customize the output.

Using Simple ls Command Options

There will come a time when a user will want to know the last file touched, the last file changed or maybe the largest or smallest file within a directory. This type of search can be performed with the ls command. Previously the ls command was used to display directories and files within directories, but by using some of the ls command options and piping the output of ls to the head command to limit the number of displayed lines we can find some of these more specific results.

The following home directory is used for the next few examples. Using the –A option makes ls show files beginning with . but eliminates the . and .. files from the display.

$ ls -Al
total 44
-rw------- 1 tclark tclark 7773 Feb 2 17:11 .bash_history
-rw-r--r-- 1 tclark tclark 24 Aug 18 11:23 .bash_logout
-rw-r--r-- 1 tclark tclark 191 Aug 18 11:23 .bash_profile
-rw-r--r-- 1 tclark tclark 124 Aug 18 11:23 .bashrc
-rw-r--r-- 1 tclark tclark 237 May 22 2003 .emacs
-rw-rw-r-- 1 tclark tclark 0 Feb 3 09:00 example1.fil
-rw-rw-r-- 1 tclark tclark 0 Jan 13 21:13 example2.xxx
drwxrwxr-x 2 tclark authors 4096 Jan 27 10:17 examples
-rw-r--r-- 1 tclark tclark 120 Aug 24 06:44 .gtkrc
drwxr-xr-x 3 tclark tclark 4096 Aug 12 2002 .kde
-rw-r--r-- 1 tclark authors 0 Jan 27 00:22 umask_example.fil
-rw------- 1 tclark tclark 876 Jan 17 17:33 .viminfo
-rw-r--r-- 1 tclark tclark 220 Nov 27 2002 .zshrc

Finding the File Last Touched (Modified) in a Directory

The –t option is used to sort the output of ls by the time the file was modified. Then, the first two lines can be listed by piping the ls command to the head command.

$ ls -Alt|head -2
total 44
-rw-rw-r-- 1 tclark tclark 0 Feb 3 09:00 example1.fil

Using the pipe (|) character in this way tells Linux to take the output of the command preceding the pipe and use it as input for the second command. In this case, the output of ls –Alt is taken and passed to the head -2 command which treats the input just like it would a text file. This type of piping is a common way to combine commands to do complex tasks in Linux.
Finding the File with the Last Attribute Change

The –c option changes ls to display the last time there was an attribute change of a file such as a permission, ownership or name change.

$ ls -Alct|head -2
total 44
-rw-rw-r-- 1 tclark tclark 0 Feb 3 09:07 example1.fil

Again we are using the head command to only see the first two rows of the output. While the columns for this form of the ls command appear identical the date and time in the output now reflect the last attribute change. Any chmod, chown, chgrp or mv operation will cause the attribute timestamp to be updated.

Finding the File Last Accessed in a Directory

Beyond file and attribute modifications we can also look at when files were last accessed. Using the –u option will give the time the file was last used or accessed.

$ ls -Alu|head -2
total 44
-rw------- 1 tclark tclark 7773 Feb 3 08:56 .bash_history

Any of these ls commands could be used without the |head -2 portion to list information on all files in the current directory.

Finding the Largest Files in a Directory

The –S option displays files by their size, in descending order. Using this option and the head command this time to see the first four lines of output we can see the largest files in our directory.

$ ls -AlS|head -4
total 44
-rw------- 1 tclark tclark 7773 Feb 2 17:11 .bash_history
drwxrwxr-x 2 tclark authors 4096 Jan 27 10:17 examples
drwxr-xr-x 3 tclark tclark 4096 Aug 12 2002 .kde

Finding the Smallest Files in a Directory

Adding the –r option reverses the display, sorting sizes in ascending order.

$ ls -AlSr|head -4
total 44
-rw-r--r-- 1 tclark authors 0 Jan 27 00:22 umask_example.fil
-rw-rw-r-- 1 tclark tclark 0 Jan 13 21:13 example2.xxx
-rw-rw-r-- 1 tclark tclark 0 Feb 3 09:00 example1.fil

The –r option can also be used with the other options discussed in this section, for example to find the file which has not been modified or accessed for the longest time.

Use of the ls command options is acceptable when the user is just interested in files in the current working directory, but when we want to search over a broader structure we will use the find command.

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


unix, linux, system administration, sysadmin

APC-UPS ES 650A good Uninterrupted Power Supply (UPS) can keep your equipment running through short power outages. It is a good idea no matter where you work from, but especially if you work from home. Here’s why:

Where I live power outages are common but short. Now, my primary machine is a laptop, so that will run for hours on battery (well, at least an hour), but since I need my cable modem and wireless router to connect to client systems if those devices loose power I’m basically out of business.

As I mentioned we have frequent power outages here in Concord, NH, USA but they are typically only a few seconds. That wouldn’t be a problem at all, except even a short power blip means I loose all my SSH connections! That can cause a 5 second power outage to cost me 15 minutes of work or more!

So, what do I have? Well, I’m partial to APC UPSs. They are what most of the data centers I have worked with use and they’ve got a great industry reputation.

Choosing a UPS

There are two major factors when choosing a UPS, Wattage and Volt-Ampres.

Wattage determines how much you can hook up to a given UPS. Devices generally give a wattage rating somewhere in the specs and power adapters often have them listed right on them. You’ll want to add together all the devices you wish to UPS (don’t forget about monitors) and purchase a UPS of at least that wattage, and probably a bit more.

Let’s say you have two computers which consume 85 watts, a monitor which consumes 120 watts, a cable modem which consumes 15 watts and a wireless router which consumes 7 watts (this is about my configuration.) That means I need a UPS which will support a maximum draw of at least 312 watts.

The Volt-Ampre calculation is a bit more complicated. This will determine how long the UPS will be able to supply power to your devices.

Correction: I had originally mistaken Volt-Ampre for Amp-Hours. Volt-Ampre is actually similar to watts except VA is more accurate for the complex power consumption in our computers. Higher is still better, but it doesn’t mean the UPS will necessarily last longer.

To determine the capacity of a UPS we would need to know the Amp-Hours of the battery. Unfortunately most (if not all) producers fail to publish this information so we the consumers are left trusting the manufacturers documentation to determine duration.

I ended up going with the APC Backup-UPS ES 650. At 65 VA and 450 max wattage for under $100 it was the right balance of cost and capacity for me. The delivered software also allows the unit to be connected via USB to a Mac or PC to adjust power management when running on the UPS similarly to how you can have different power settings on a laptop for when you are running on battery.

There are a few special permission mode settings that are worthy of noting. Note that the Set UID and Set GID permissions are disabled in some operating systems for security reasons.

Mode Description
Sticky bit Used for shared directories to prevent users from renaming or deleting each others’ files. The only users who can rename or delete files in directories with the sticky bit set are the file owner, the directory owner, or the super-user (root). The sticky bit is represented by the letter t in the last position of the other permissions display.
SUID Set user ID, used on executable files to allow the executable to be run as the file owner of the executable rather than as the user logged into the system.
SUID can also be used on a directory to change the ownership of files created in or moved to that directory to be owned by the directory owner rather than the user who created it.
SGID Set group ID, used on executable files to allow the file to be run as if logged into the group (like SUID but uses file group permissions).
SGID can also be used on a directory so that every file created in that directory will have the directory group owner rather than the group owner of the user creating the file.

The following example displays the SUID permission mode that is set on the passwd command, indicated by the letter s in the last position of the user permission display. Users would like to be able to change their own passwords instead of having to ask the System Administrator to do it for them. Since changing a password involves updating the /etc/passwd file which is owned by root and protected from modification by any other user, the passwd command must be executed as the root user.

The which command will be used to find the full path name for the passwd command, then the attributes of the passwd command will be listed, showing the SUID permission(s).

$ which passwd
/usr/bin/passwd
$ ls -l /usr/bin/passwd
-r-s--x--x 1 root root 17700 Jun 25 2004 /usr/bin/passwd

Here we see not only that the SUID permissions are set up on the passwd command but also that the command is owned by the root user. These two factors tell us that the passwd command will run with the permissions of root regardless of who executes it.

These special modes can be very helpful on multi-user systems. To set or unset the sticky bit use the the t option with the chmod command. When setting the sticky bit we do not have to specify if it is for user, group or other. In the following example we will make a directory called public which anyone can write to but we’ll use the sticky bit to make sure only the file owners can remove their own files.

$ mkdir public
$ chmod 777 public
$ chmod +t public
$ ls -l
total 4
drwxrwxrwt 2 tclark authors 4096 Sep 14 10:45 public

We see that the last character of the permissions string has a t indicating the sticky bit has been set. We could also prefix the number 1 to the chmod command using the number to achieve the same results. The following chmod command will accomplish the same thing as the two chmod commands in the last example:

$ chmod 1777 public
$ ls -l
total 4
drwxrwxrwt 2 tclark authors 4096 Sep 14 10:45 public

Now let’s say we instead want to make a directory which other users can copy files but which we want the files to instantly become owned by our username and group. This is where the SUID and SGID options come in.

$ mkdir drop_box
$ chmod 777 drop_box
$ chmod u+s,g+s drop_box
$ ls -l
total 4
drwsrwsrwx 2 tclark authors 4096 Sep 14 10:55 drop_box

Now anyone can move files to this directory but upon creation in drop_box they will become owned by tclark and the group authors. This example also illustrates how you can change multiple levels of permissions with a single command by separating them with a comma. Just like with the other permissions this could have been simplified into one command using the SUID and SGID numeric values (4 and 2 respectively.) Since we are changing both in this case we use 6 as the first value for the chmod command.

$ chmod 6777 drop_box/
$ ls -l
total 4
drwsrwsrwx 2 oracle users 4096 Sep 14 10:55 drop_box

Easy Linux CommandsFor more tips like this check out my book Easy Linux Commands, only $19.95 from Rampant TechPress.

Buy it now!


unix, linux, system administration, sysadmin

« Previous PageNext Page »