locate command
tools - This finds files by name the easy way. It performs a rapid database search of pathnames, and then outputs every name that matches a given substring
find command
tools - Search for files the hard way by using a directory hierarchy. This searches a given directory (and its subdirectories) for files based on a variety of attributes. It can be used to identify files that meet specific criteria. It does this through t
xargs command
command - Build and execute command lines from standard input
touch command
command - Change file times
stat command
command - Display file or file system status
Locate Example
example - [me@linuxbox ~]$ locate bin/zip
Locate With Grep Example
example - [me@linuxbox ~]$ locate zip | grep bin
Find Example
example - [me@linuxbox ~]$ find ~
Find with Piping Example
example - [me@linuxbox ~]$ find ~ | wc -l
Find with a type -d test Example
example - [me@linuxbox ~]$ find ~ -type d | wc -l
This type of test used with the find searching will locate only directories
Find with a type -f test Example
example - [me@linuxbox ~]$ find ~ -type f | wc -l
This type of test used with the find searching will locate only regular files
Find Files Testing parameters
Find Test Parameters -
File Type .....Description
b ......................Block special device file
c ......................Character special device file
d ......................Directory
f .......................Regular file
l .......................Symb
Find with -type -name & wildcard Example
example - [me@linuxbox ~]$ find ~ -type f -name "*.JPG" -size +1M | wc -l
In this example, we add the -name test followed by the wildcard pattern. Notice how we enclose it in quotes to prevent pathname expansion by the shell. Next, we add the -size test f
Find with -cmin n Test
Test - Match files or directories whose content or attributes were last modified exactly n minutes ago. To specify less than n minutes ago, use -n, and to specify more than n minutes ago, use +n.
Find with -cnewer file Test
Test - Match files or directories whose contents or attributes were last modified more recently than those of file.
Find with -ctime n Test
Test - Match files or directories whose contents or attributes were last modified n*24 hours ago.
Find with -empty Test
Test - Match empty files and directories.
Find with -group name Test
Test - Match file or directories belonging to group. group may be expressed either as a group name or as a numeric group ID.
Find with -iname pattern Test
Test - Like the -name test but case-insensitive.
Find with -inum n Test
Test - Match files with inode number n. This is helpful for finding all the hard links to a particular inode.
Find with -mtime n Test
Test - Match files or directories whose contents were last modified n*24 hours ago.
Find with -name pattern Test
Test - Match files and directories with the specified wildcard pattern.
Find with -newer file Test
Test - Match files and directories whose contents were modified more recently than the specified file. This is useful when writing shell scripts that perform file backups. Each time
you make a backup, update a file (such as a log) and then use find to det
Find with -nouser Test
Test - Match file and directories that do not belong to a valid user. This can be used to find files belonging to deleted accounts or to detect activity by attackers.
Find with -nogroup Test
Test - Match files and directories that do not belong to a valid group.
Find with -perm mode Test
Test - Match files or directories that have permissions set to the specified mode. mode can be expressed by either octal or symbolic notation.
Find with -samefile name Test
Test - Similar to the -inum test. Match files that share the same inode number as file name.
Find with -size n Test
Test - Match files of size n.
Find with -type c Test
Test - Match files of type c.
Find with -user name Test
Test - Match files or directories belonging to user name. The user may be expressed by a username or by a numeric user ID.
Find with -mmin n Test
Test - Match files or directories whose contents were last modified n minutes ago.
Find with -and Logical Operator Test
Test - Match if the tests on both sides of the operator are true. This can be shortened to -a. Note that when no operator is present, -and is implied by default
Find with -or Logical Operator Test
Test - Match if a test on either side of the operator is true. This can be shortened to -o
Find with -not Logical Operator Test
Test - Match if the test following the operator is false. This can be abbreviated with an exclamation point (!)
Find with ( ) Logical Operator Test
Test - Groups tests and operators together to form larger expressions. This is used to control the precedence of the logical evaluations. By default, find evaluates from left to right. It is often necessary to override the default evaluation order to obta
Find using the -or Logical Operator Example
example - ( expression 1 ) -or ( expression 2 )
Example of Using the ""or"" logical Operator
[me@linuxbox ~]$ find ~ \( -type f -not -perm 0600 \) -or \( -type d-not -perm 0700 \)
Find using the -delete Action
Action - Delete the currently matching file.
Find using the -ls Action
Action - Perform the equivalent of ls -dils on the matching file. Output is sent to standard output.
Find using the -print Action
Action - Output the full pathname of the matching file to standard output. This is the default action if no other action is specified.
Find using the -quit Action
Action - Quit once a match has been made.
Find using the ~ Action Example
Example - find ~
This produced a list of every file and subdirectory contained within our home directory. It produced a list because the -print action is implied if no other action is specified. Thus, our command could also be expressed as follows: find ~
Find using the -delete Action Example
example - find ~ -type f -name '*.bak' -delete
We can use find to delete files that meet certain criteria. For example, to delete files that
have the file extension .bak (which is often used to designate backup files).
In this example, every file in the u
Find using the -print Action Example
example - find ~ -type f -name '*.bak' -print
This command will look for every regular file (-type f) whose name
ends with .bak (-name '*.bak') and will output the relative pathname of each
matching file to standard output (-print).
But this has the ""and
Exec Command
command - exec rm '{}' ';'
This command is an arbitrary command which uses the {} brackets as a symbolic representation of the current
pathname, and the semicolon is a required delimiter indicating the end of the command.
Since the brace and semicolon cha
Find using the -exec Command and the ""-OK"" Action
example - find ~ -type f -name 'foo*' -ok ls -l '{}' ';'
< ls ... /home/me/bin/foo > ? y
-rwxr-xr-x 1 me me 224 2007-10-29 18:44 /home/me/bin/foo
< ls ... /home/me/foo.txt > ? y
-rw-r--r-- 1 me me 0 2016-09-19 12:53 /home/me/foo.txt
In this example, we se
xargs Command
command - command performs an interesting function. It accepts input from standard input and converts it into an argument list for a specified command.
Find using the -xargs Command Example
example - ""find ~ -type f -name 'foo*' -print | xargs ls -l
-rwxr-xr-x 1 me me 224 2007-10-29 18:44 /home/me/bin/foo
-rw-r--r-- 1 me me 0 2016-09-19 12:53 /home/me/foo.txt
Here we see the output of the find command piped into xargs, which, in turn, const
Find using the -depth Option
Option - Direct find to process a directory's files before the directory itself. This option is automatically applied when the -delete action is specified.
Find using the -maxdepth levels Option
Option - Set the maximum number of levels that find will descend into a directory tree when performing tests and actions.
Find using the -mindepth levels Option
Option - Set the minimum number of levels that find will descend into a directory tree before applying tests and actions.
Find using the -mount Option
Option - Direct find not to traverse directories that are mounted on other file systems.
Find using the -noleaf Option
Option - Direct find not to optimize its search based on the assumption that it is searching a Unix-like file system. This is needed when scanning DOS/Windows file systems and CD-ROMs.
gzip & gunzip command
command - Compress or expand files (unzip uncompresses)
GNU Zip; fast, portable; mostpopular; .gz file suffix
bzip2 & bunzip2 command
command - A block sorting file compressor (unzip uncompresses)
BZip; better compression;.bz2 file suffix
compress command
command - Compress or expand files (unzip uncompresses)
traditional unix; .Z file suffix
zip command
command - Package and Compress or expand files (unzip uncompresses)
Windows Zip format; .zip file suffix
uuendode command
command - Compress or expand files (unzip uncompresses)
uuencoded (replaced by MIME)
tar command
command - Tape archiving utility
rsync command
command - Remote file and directory synchronization
Compressing files Definition
Definition - This is the process of removing redundancy from data.
These algorithms (the mathematical techniques used to carry out the compression)
fall into two general categories.
Lossless and Lossy
lossless compression Definition
Definition - This type of compression preserves all the data contained in the original. This is the only one a computer can tolerate (No computer can tolerate lossed data).
This means that when a file is restored from a compressed version, the restored
fi
lossy compression Definition
Definition - This type of compression removes data as the compression is
performed to allow more compression to be applied. When a lossy file is restored,
it does not match the original version; rather, it is a close approximation. Examples of lossy compr
gzip Definition
Definition - This command program is used to compress one or more files. When executed, it replaces the
original file with a compressed version of the original. The corresponding gunzip program is used to restore compressed files to their original, uncomp
gzip & gunzip Example
example - [me@linuxbox ~]$ ls -l /etc > foo.txt
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me me 15738 2018-10-14 07:15 foo.txt
[me@linuxbox ~]$ gzip foo.txt
[me@linuxbox ~]$ ls -l foo.*
-rw-r--r-- 1 me me 3230 2018-10-14 07:15 foo.txt.gz
[me@linuxbox ~]$
gzip -c Option
Option - Write output to standard output and keep the original files.
Long option can be written as followed:
--stdout
--to-stdout
gzip -d Option
Option - Decompress. This causes gzip to act like gunzip.
Long option can be written as followed:
--decompress
--uncompress
gzip -f Option
Option - Force compression even if a compressed version of the original file already exists.
Long option can be written as followed:
--force
gzip -h Option
Option - Display usage information.
Long option can be written as followed:
--help
gzip -l Option
Option - List compression statistics for each file compressed.
Long option can be written as followed:
--list
gzip -r Option
Option - If one or more arguments on the command line is a directory, recursively compress files contained within them.
Long option can be written as followed:
--recursive
gzip -t Option
Option - Test the integrity of a compressed file.
Long option can be written as followed:
--test
gzip -v Option
Option - Display verbose messages while compressing.
Long option can be written as followed:
--verbose
gzip -number Option
Option - Set amount of compression. number is an integer in the range of 1 (fastest, least compression) to 9 (slowest, most compression). The values 1 and 9 may also be expressed as --fast and --best, respectively. The default value is 6.
bzip2 Example
example - [me@linuxbox ~]$ ls -l /etc > foo.txt
[me@linuxbox ~]$ ls -l foo.txt
-rw-r--r-- 1 me me 15738 2018-10-17 13:51 foo.txt
[me@linuxbox ~]$ bzip2 foo.txt
[me@linuxbox ~]$ ls -l foo.txt.bz2
-rw-r--r-- 1 me me 2792 2018-10-17 13:51 foo.txt.bz2
[me@lin
Archiving Definition
Definition - This is a common file-management task often used in conjunction with compression.
It is the process of gathering up many files and bundling them together into a single large file. It is often done as part of system backups. It is also used wh
tar Definition
Definition - This is the classic tool for archiving files.
Its name, short for tape archive, reveals its roots as a tool for making backup tapes.
While it is still used for that traditional task, it is equally adept on other storage devices.
We often see
tar using the -c Mode
Mode - Create an archive from a list of files and/or directories.
tar using the -x Mode
Mode - Extract an archive.
tar using the -r Mode
Mode - Append specified pathnames to the end of an archive.
tar using the -t Mode
Mode - List the contents of an archive.
tar Example
example - [me@linuxbox ~]$ tar cf playground.tar playground
This command creates a tar archive named playground.tar that contains the entire
playground directory hierarchy. We can see that the mode and the f option, which is used
to specify the name of th
tar using the -t Mode Example
example - [me@linuxbox ~]$ tar tf playground.tar
This Example lists the contents of the archive
tar using the -tv Mode Example
example - [me@linuxbox ~]$ tar tvf playground.tar
This Example lists the contents of the archive with extra detailed listing by using the verbose option
tar using the -x Mode Example
example - [me@linuxbox ~]$ mkdir foo
[me@linuxbox ~]$ cd foo
[me@linuxbox foo]$ tar xf ../playground.tar
[me@linuxbox foo]$ ls
playground
This example let's extract the playground in a new location. We will do this by creating a new directory named foo, c
zip Definition
Definition - This program is both a compression tool and an archiver. The file format used by the program is familiar to Windows users, as it reads and writes .zip files. In Linux, however, gzip is the predominant compression program, with bzip2 being a c
zip Command Example
Example - [me@linuxbox ~]$ zip -r playground.zip playground
In this Example, we include the -r option for recursion. If you dont use the -r, only the playground directory is stored. Although the addition of the extension .zip is automatic,
we will include
unzip Command Example
Example - [me@linuxbox ~]$ cd foo
[me@linuxbox foo]$ unzip ../playground.zip
rsync Definition
Definition -
rsync Example
Example - [me@linuxbox ~]$ rsync -av playground foo
This command is invoked like this:
rsync options source destination
where source and destination are one of the following:
? A local file or directory
? A remote file or directory in the form of [user@]h
Regular Expressions Definition
Definition - These are symbolic notations used to identify patterns in text. In some ways, they resemble the shell's wildcard method of matching file and pathnames but on a much grander scale. They are supported by many command line tools and by most prog
grep Program Definition
Definition - This is actually derived from the phrase "global regular expression print," so we can see that this program has something to do with regular expressions. In essence, this program searches text files for the occurrence text matching a specifie
grep with ""-i"" option
Option - Ignore case. Do not distinguish between uppercase and lowercase characters.
Long option can be written as followed:
--ignore-case
grep with ""-v"" option
Option - Invert match. Normally, grep prints lines that contain a match.
This option causes grep to print every line that does not contain amatch.
Long option can be written as followed:
--invert-match
grep with ""-c"" option
Option - Print the number of matches (or non-matches if the -v option is also specified) instead of the lines themselves.
Long option can be written as followed:
--count
grep with ""-l"" option
Option - Print the name of each file that contains a match instead of the lines themselves.
Long option can be written as followed:
--files-with-matches
grep with ""-L"" option
Option - Like the -l option, but print only the names of files that do not contain matches.
Long option can be written as followed:
--files-without-match
grep with ""-n"" option
Option - Prefix each matching line with thenumber of the line within the file.
Long option can be written as followed:
--line-number
grep with ""-h"" option
Option - For multi-file searches, suppress the output of filenames.
Long option can be written as followed:
--no-filename
grep Example
example - [me@linuxbox ~]$ ls /usr/bin | grep zip
This will list all the files in the /usr/bin directory whose names contain the substring zip.
grep with ""-l"" option example
example - [me@linuxbox ~]$ grep -l bzip dirlist*.txt
dirlist-bin.txt
In this example, we use this to list the files that contained the matches rather than the matches themselves by using the -l option.
grep with ""-L"" option example
example - [me@linuxbox ~]$ grep -L bzip dirlist*.txt
dirlist-sbin.txt
dirlist-usr-bin.txt
dirlist-usr-sbin.txt
In this example, we use this to list only the files that did not contain a match by using the -L option.
Metacharacters Characters
Characters - ^ $ . [ ] { } - ? * + ( ) | \
Literal Characters
Characters - A - Z & 1-9
grep with ""."" metacharacter example
example - [me@linuxbox ~]$ grep -h '.zip' dirlist*.txt
bunzip2
bzip2
bzip2recover
gunzip
gzip
funzip
gpg-zip
preunzip
prezip
prezip-bin
unzip
unzipsfx
Metacharacter "." Definition
Definition - This metacharacter is used with grep to match any character. If we include it in a regular expression, it will match any character in that character position.
Metacharacter ""^"" Definition
Definition - This metacharacter is used with grep as an anchor in an regular expression. This means they cause the match to occur only if the regular expression is found at the
beginning of the line.
Metacharacter ""$"" Definition
Definition - This metacharacter is used with grep as an anchor in an regular expression. This means they cause the match to occur only if the regular expression is found at the
end of the line.
"Metacharacter ""[]"" Definition
Definition - This metacharacter is used with grep to match a single character from a specified set of characters. Using this we can specify a set of characters (including characters that would otherwise be interpreted as metacharacters) to be matched.
NOT
Metacharacter ""^"" Example
example - [me@linuxbox ~]$ grep -h '^zip' dirlist*.txt
zip
zipcloak
zipgrep
zipinfo
zipnote
zipsplit
Metacharacter ""$""Example
example - [me@linuxbox ~]$ grep -h 'zip$' dirlist*.txt
gunzip
gzip
funzip
gpg-zip
preunzip
prezip
unzip
zip
Metacharacter ""*"" Example
example - [me@linuxbox ~]$ grep -h '[bg]zip' dirlist*.txt
bzip2
bzip2recover
gzip
Literal ""|"" Example
example - [me@linuxbox ~]$ echo ""AAA"" | grep -E 'AAA|BBB'
AAA
[me@linuxbox ~]$ echo ""BBB"" | grep -E 'AAA|BBB'
BBB
[me@linuxbox ~]$ echo ""CCC"" | grep -E 'AAA|BBB'
[me@linuxbox ~]$
Here we see the regular expression 'AAA|BBB', which means "match eithe
Metacharacter ""[ ]"" WITH A NEGATION ^ INSIDE Example
example - [me@linuxbox ~]$ grep -h '[^bg]zip' dirlist*.txt
bunzip2
gunzip
funzip
gpg-zip
preunzip
prezip
prezip-bin
unzip
unzipsfx
Metacharacter ""[ ]"" WITH A RANGE - INSIDE Example
example - [me@linuxbox ~]$ grep -h '^[A-Z]' dirlist*.txt
MAKEDEV
ControlPanel
GET
HEAD
POST
X
X11
Xorg
MAKEFLOPPIES
NetworkManager
NetworkManagerDispatcher
NOTE: THIS IS THE SAME THING AS THIS:
[me@linuxbox ~]$ grep -h '^[ABCDEFGHIJKLMNOPQRSTUVWXZY]' dirl
Metacharacter ""[]"" WITH A RANGE - IN FRONT BUT INSIDE Example
example - [me@linuxbox ~]$ grep -h '[-AZ]' dirlist*.txt
This will match every filename containing a dash, or an uppercase A or an uppercase Z:
grep used with echo Example
example - [me@linuxbox ~]$ echo ""AAA"" | grep AAA
AAA
[me@linuxbox ~]$ echo ""BBB"" | grep AAA
[me@linuxbox ~]$
In this Example, using echo with the grep we pipe the output of echo into grep and see the results. When a match occurs, we see it printed out
Alternation Definition
Definition - This is the facility that allows a match to occur from among a set of expressions. Just as a bracket expression allows a single character to match from a set of specified characters, this allows matches from a set of strings or other regular
Metacharacter Quantifier ? Definition
Definition - This quantifier means, in effect, "Make the preceding element optional." Let's say we wanted to check a phone number for validity and we considered a phone number to be valid if it matched either of these two forms, where n is a numeral:
(nnn
Metacharacter Quantifier ? Example
example - [me@linuxbox ~]$ echo ""(555) 123-4567"" | grep -E '^\(?[0-9][0-9][0-9]
\)? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$'
(555) 123-4567
[me@linuxbox ~]$ echo ""555 123-4567"" | grep -E '^\(?[0-9][0-9][0-9]\)
? [0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]$'
55
Metacharacter Quantifier * Definition
Definition - This quantifier is used to denote an optional item; however, unlike the ?, the item may occur any number of times, not just once. Let's say we wanted to see whether a string was a sentence; that is, it starts with an uppercase letter, then co
Metacharacter Quantifier * Example
example - [me@linuxbox ~]$ echo ""This works."" | grep -E '[[:upper:]][[:upper:][:lower:] ]*\.'
This works.
[me@linuxbox ~]$ echo ""This Works."" | grep -E '[[:upper:]][[:upper:][:lower:] ]*\.'
This Works.
[me@linuxbox ~]$ echo ""this does not"" | grep -E
Metacharacter Quantifier + Definition
Definition - This metacharacter quantifier works much like the *, except it requires at least one instance of the preceding element to cause a match. Here is a regular expression that will match only the lines consisting of groups of one or more alphabeti
Metacharacter Quantifier + Example
Example - [me@linuxbox ~]$ echo ""This that"" | grep -E '^([[:alpha:]]+ ?)+$'
This that
[me@linuxbox ~]$ echo ""a b c"" | grep -E '^([[:alpha:]]+ ?)+$'
a b c
[me@linuxbox ~]$ echo ""a b 9"" | grep -E '^([[:alpha:]]+ ?)+$'
[me@linuxbox ~]$ echo ""abc d"" |
Metacharacter Quantifier { } Definition
Definition - This metacharacter quantifier is used to express minimum and maximum numbers of required matches. They may be specified in four possible ways as outlined in Table 19-3.
Specifier ........Meaning
{n} .................Match the preceding elemen
Metacharacter Quantifier { } Example
Example - [me@linuxbox ~]$ echo ""(555) 123-4567"" | grep -E '^\(?[0-9]{3}\)? [0-
9]{3}-[0-9]{4}$'
(555) 123-4567
[me@linuxbox ~]$ echo ""555 123-4567"" | grep -E '^\(?[0-9]{3}\)? [0-9]
{3}-[0-9]{4}$'
555 123-4567
[me@linuxbox ~]$ echo ""5555 123-4567"" |