- Home
- Services
- Software
- Hosted Sites
- Contact

Linux/Unix Tips

With over 20 years of Unix experience, there are a number of questions/topics that come up over and over again in technical forums. This list is an attempt to summarise them and provide a useful resource for people with the same questions.

The find command

The find command is incredibly useful and probably accounts for more questions that any other Unix utility. There are a few areas of confusion that regually come up.

Quite often there is a question related to finding files based on their creation time. Unix/Linux filesystems do not store the creation time of a file. There are three categories you can use:

  1. atime - Timestamp when the file was last accessed
  2. ctime - Timestamp of when the file was last changed. ctime is quite often misunderstood to be creation time, but that is incorrect. Some of the actions that will update the ctime of a file are: modification of the contents, changing ownership/permissions.
  3. mtime - Timestamp when the file was last modified. For most practical purposes, this suffices for creation time.

Related to above, are the time specifications for the -atime, -ctime and -mtime options. All too often, you will see an example like:

find /dir -type -f -mtime +1

where the it is stated that this command will find files older than 1 day. This is not quite correct. It actually means older than 48 hours from the current time (in effect, files older than 2 days).

To find files older than one day, you would use:

find /dir -type f -mtime +0

The prefix to the number makes all the difference. The following should hopefully make it clear:

-mtime 1Modified 24 hours ago
-mtime -1Modified within the last 48 hours
-mitme +1Modified older than 48 hours ago.

Loops

Quite often, people will try to read lines from a file with a 'for' loop and wonder why they get one word at a time. A 'for' loop interates over whitespace (by default) separated items. The separator is determined by the specicial IFS (Internal Field Separator) environment variable. To read a line at a time from a file, you should use a while loop, eg:

while read line
do
  echo "$line"
done 

An example of

IFS=","
for field in `cat /some/file`
do
  echo "$field"
done

where /some/file contains

one,two,three
four,five,six,seven

The output will be: one two three four five six seven





Ginini Technologies Ltd