Categories: Command-Line, General, Linux

Chris Manning

Share

Using Find

When no arguments are given, find lists all files in the current directory and all of its subdirectories. Commonly used options to shorten the list include -name (only list files with a certain pattern in their name), -iname (also ignore the case of file names), and -type (which will restrict the results to files of a certain specified type, such as d for directory, l for symbolic link or f for a regular file, etc).

Searching for files and directories named “gcc”:
$ find /usr -name gcc

Searching only for directories named “gcc”:
$ find /usr -type d -name gcc

Searching only for regular files named “test1”:
$ find /usr -type f -name test1

Finding Files Based on Time and Size

It is sometimes the case that you wish to find files according to attributes such as when they were created, last used, etc, or based on their size. Both are easy to accomplish.

Finding based on time:
$ find / -ctime 3

Here, -ctime is when the inode meta-data (i.e., file ownership, permissions, etc) last changed; it is often, but not necessarily when the file was first created. You can also search for accessed/last read (-atime) or modified/last written (-mtime) times. The number is the number of days and can be expressed as either a number (n) that means exactly that value, +n which means greater than that number, or -n which means less than that number. There are similar options for times in minutes (as in -cmin, -amin, and -mmin).

Finding based on sizes:

$ find / -size 0

Note the size here is in 512-byte blocks, by default; you can also specify bytes (c), kilobytes (k), megabytes (M), gigabytes (G), etc. As with the time numbers above, file sizes can also be exact numbers (n), +n or -n. For details consult the man page for find.

For example, to find files greater than 10 MB in size and running a command on those files:
$ find / -size +10M -exec command {} ’;’

Locate

The locate utility program performs a search through a previously constructed database of files and directories on your system, matching all entries that contain a specified character string. This can sometimes result in a very long list.

To get a shorter more relevant list we can use the grep program as a filter; grep will print only the lines that contain one or more specified strings as in:

$ locate zip | grep bin

which will list all files and directories with both “zip” and “bin” in their name . (We will cover grep in much more detail later.) Notice the use of | to pipe the two commands together.

locate utilizes the database created by another program, updatedb. Most Linux systems run this automatically once a day. However, you can update it at any time by just running updatedb from the command line as the root user.

 

 

Wildcards and Matching File Names

You can search for a filename containing specific characters using wildcards.

WildcardResult
?Matches any single character
*Matches any string of characters
[set]Matches any character in the set of characters, for example [adf] will match any occurrence of “a”, “d”, or “f”
[!set]Matches any character not in the set of characters

To search for files using the ? wildcard, replace each unknown character with ?, e.g. if you know only the first 2 letters are ‘ba’ of a 3-letter filename with an extension of .out, type ls ba?.out .

To search for files using the * wildcard, replace the unknown string with *, e.g. if you remember only that the extension was .out, type ls *.out

You can also use set like this to find characters alphabetically between two characters: ba[a-s].out will give you all files names with the third letter being between a and s.

 

Hard and Soft (Symbolic) Links

LFS01_ch06_screen21aln can be used to create hard links and (with the -s option) soft links, also known as symbolic links or symlinks. These two kinds of links are very useful in UNIX-based operating systems. The advantages of symbolic links are discusssed on the following screen.

Suppose that file1 already exists. A hard link, called file2, is created with the command:

$ ln file1 file2

Note that two files now appear to exist. However, a closer inspection of the file listing shows that this is not quite true.

$ ls -li file1 file2

The -i option to ls prints out in the first column the inode number, which is a unique quantity for each file object. This field is the same for both of these files; what is really going on here is that it is only one file but it has more than one name associated with it,  as is indicated by the 3 that appears in the ls output.  Thus, there already was another object linked to file1 before the command was executed.

 

Symbolic Links

LFS01_ch06_screen23aSymbolic (or Soft) links are created with the -s option as in:

$ ln -s file1 file4
$ ls -li file1 file4

Notice file4 no longer appears to be a regular file, and it clearly points to file1 and has a different inode number.

Symbolic links take no extra space on the filesystem (unless their names are very long). They are extremely convenient as they can easily be modified to point to different places. An easy way to create a shortcut from your home directory to long pathnames is to create a symbolic link.

Unlike hard links, soft links can point to objects even on different filesystems (or partitions) which may or may not be currently available or even exist. In the case where the link does not point to a currently available or existing object, you obtain a dangling link.

Hard links are very useful and they save space, but you have to be careful with their use, sometimes in subtle ways. For one thing if you remove either file1 or file2 in the example on the previous screen, the inode object (and the remaining file name) will remain, which might be undesirable as it may lead to subtle errors later if you recreate a file of that name.

If you edit one of the files, exactly what happens depends on your editor; most editors including vi and gedit will retain the link by default but it is possible that modifying one of the names may break the link and result in the creation of two objects.

Installing Software

Operation

RPM

Deb

Install a package

rpm –i foo.rpm

dpkg –install foo.deb

Install a package with dependencies from repository

yum install foo

apt-get install foo

Remove a package

rpm –e foo.rpm

dpkg –remove foo.deb

Remove a package and dependencies using repository

yum remove foo

apt-get remove foo

Update package to a newer version

rpm –U foo.rpm

dpkg –install foo.deb

Update package using repository and resolving dependencies

yum update foo

apt-get upgrade foo

Update entire system

yum update

apt-get dist-upgrade

Show all installed packages

rpm –qa or yum list installed

dpkg –list

Get information about an installed package including files

rpm –qil foo

Dpkg –listfiles foo

Shaow available package with “foo” in name

yum list foo

apt-cache search foo

Show all available packages

yum list

apt-cache dumpavail

What packages does a file belong to?

rpm –qf file

dpkg –search file

 

 

 

 

Leave A Comment

Related Posts