This post is Part Three of the summary of some common Linux Shell Commands. The commands we are going to use today are: find
, locate
, gzip
, tar
.
Searching Files: find
Usage
find <PATH> <Expression>
Here <Expression>
contains 4 types:
- options
- tests (T / F)
- actions
- operators
e.g. find /etc -size +100k
Find files that size > 100k, under the directory /etc
. Note that:
+n
: ≥ nn
: = n-n
: ≤ n
Wildcard Chars
Four Common Types
*
: Match 0, or multiple chars?
: Match any one char[STRING]
: Match any char in the string.- e.g.
[a-z0-9]
matches lowercase letters and digits - Note that
[! STRING]
means match any other chars except ones after!
- e.g.
\
: escape
Using Wildcard with find
:
-iname
: ignore casesfind /usr -name "te*sh"
: find all files types under/usr
, starting with te, ending with sh.-path
&-wholename
: find pathsfind /usr -wholename /usr/bin
: find bin under/usr
directory. Note that the 2 directories, path prefix must be the same, and must be whole paths.-regex
&-iregex
Links
Symbolic Links (Soft Links):
-lname
&-ilname
: Test. Check if a file is a symbolic link.find /usr/bin -lname "*python3*"
: find soft links that contain python3 in the filenames.
Hard Links:
find /usr -samefile file1
: Use-samefile
to find all hard links of file1.find /usr -inum <inode>
: Each inode has a unique number, but hard links have the same inode.find /usr -links 3
: find files that have 3 hard links.
Time
Each file has three timestamps:
- Access (
-atime n
,-amin n
) - Change (
-ctime n
,-cmin n
) : Change file status - Modify (
-mtime n
,-mmin n
) : Edit file content
e.g. Modify time between 5 mins and 24 hours before.
1 | find /usr -mtime 0 -mmin +5 |
Size
-size n[bckwMG]
: n is the number (+, -)
b
: Blockc
: Chark
: KBw
: Number of 2-byte charsM
: MBG
: GB
Type
-type [dfl]
:
d
: Dir.f
: Filel
: Links
e.g. Normal file, size > 30k.
1 | sudo find /etc -type f size +30k |
Owner
-user <NAME>
-group <GROUP>
-uid n
&-gid n
e.g. find /usr -uid 0
: (Root’s UID is 0)
File Mode Bits
-readable
&-writeable
&-executable
e.g. Check the files under /etc
that can be executed by current user, ending with a.
1 | find /etc -executable -name "*a" |
-Exec
Must have {}
and \;
.
List detailed info of the files
1 | find /usr -type f -exec ls -l {} \; |
Delete all files that are modified more than 14 days ago.
1 | find /usr -type f -mtime +14 -exec rm {} \; |
Delete .log
files, and press y
to confirm, before each deletion.
1 | find /usr -name "*.log" -ok rm {} \; |
Find files start with passwd, and contains the word root.
1 | find /etc -name "passwd*" -exec grep "root" {} \; |
More Examples
Find all files (size > 12k) under /etc
, copy to /home/backup
, with original directories (-rp --parents
):
1 | sudo find /etc -type f -size +12k -exec cp -rp --parents {} /home/backup \; |
Searching Files: locate
locate
is to find results in a database that contains all files and directories in the system. The database isn’t updated in real time. To update, use:
1 | sudo updatedb |
Usage:
locate /usr/bin/python
locate --basename hello
: filename (not dir. name) contains hello
Packing Files: gzip
and tar
Usually we need to first pack the files using tar
, then zip the .tar files. Because many compressors in Linux can only compress one file each time, so it’s better to compress a packed file.
gzip
: .gz
To zip a file: gzip <FILE>
To unzip a file: gunzip <FILE>
tar
: .tar
Usage: tar <OPTION> <FILE>
-z
&--gzip
: Use gzip to compress-c
: Create-v
: detailed process-f
: filename-x
: unpack-r
: Append to end of file
e.g. Pack & zip:
1
tar -zcvf /etc/hello.tar.gz /home/hello1 /home/hello2
e.g. Unpack & unzip:
1
tar -zxvf /etc/hello.tar.gz /home