The tree
command is used to display the directory structure in a tree-like format.
-
Install Tree:
Iftree
isn't installed, you can install it with:yum install tree
-
Basic Usage:
To show the directory structure of your current location:tree
-
Display Directories Only:
To show only directories (no files):tree -d
-
Limit the Depth:
To show the directory structure up to a certain level:tree -d -L 1 # Show only level 1 directories tree -d -L 2 # Show directories up to level 2
-
Show All Files:
To show all files, including hidden ones:tree -a
-
Specify Location:
To show the directory structure of a specific path, e.g.,/var/log
:tree -d /var/log/
-
Reverse Sorting:
To display files in reverse order:tree -v -r
-
Sort by Modification Time:
To display files sorted by the last modification time:tree -v -t
The find
command is used to search for files and directories based on various criteria like name, type, user, and more.
-
Find Everything in the Current Directory:
To list all files and directories in the current directory:find .
-
Search by Name:
To search for a file or directory by its name:find . -name "user" find . -name "user*" # Wildcard - everything starting with "user"
-
Search for Files or Directories:
- Search for a file:
find / -name "data" -type f
- Search for a directory:
find / -name "data" -type d
- Search for a file:
-
Case-insensitive Search:
To search without worrying about case sensitivity (e.g., "user", "User", "USER" will all match):find / -iname "user*"
-
Find Files by User:
To find all files owned by a specific user:find / -user suraj
-
Find Files by Permission:
To find files with a specific permission (e.g.,777
):find /etc/ -perm 777
-
Redirecting Errors:
To ignore error messages (like "Permission Denied"):find / -name "passwd" 2> /dev/null
-
Find Empty Files and Directories:
To find all empty files and directories:find / -empty
-
Find Only Empty Directories:
find / -empty -type d
-
Find Only Empty Files:
find / -empty -type f
-
Find Readable Files:
To find files that are readable by the user:find / -readable
-
Find Writable Files:
To find files that can be written to:find / -writable
-
Find Executable Files:
To find files that are executable (i.e., can be run):find / -executable
-
Find Files with Specific Permissions:
- For User:
find / -perm -u=w # Files where the user has write permissions
- For Group:
find / -perm -g=w # Files where the group has write permissions
- For Others:
find / -perm -o=w # Files where others have write permissions
- For User:
The -exec
option allows you to execute another command on each result found by find
.
-
Execute a Command on Search Results: Example: Find
.log
files and then search for a string usinggrep
:find /var/log -type f -name "*.log" -exec grep "root" {} \;
-
Execute Commands Like
date
,id
,uname
:find . -exec date \; # Show the current date find . -exec id \; # Display user information find . -exec uname \; # Display system information
- Multiple Patterns Search:
You can search for multiple file patterns using logical operators:This finds files with any of the specified patterns (find / -type f \( -name "*.log" -o -name "*ple" -o -name "*pass" \)
*.log
,*ple
,*pass
).
- Limit Search Depth:
You can limit the depth of
find
to avoid searching too deeply.find / -maxdepth 2 -name "pass*"
This will search only two levels deep from the root.
-
Tree Command: Use
tree
to visualize directory structures in a tree format. -
Find Command: Use
find
to search for files and directories based on various criteria like name, type, user, permissions, and more. -
-exec: This allows you to run commands on files or directories found by
find
. -
Permissions: You can filter files based on permissions, such as writable, readable, or executable files.
These commands are essential for managing files, searching, and automating tasks in Linux.
The mlocate
command uses a database to quickly locate files on the system. This is much faster than searching the filesystem directly. However, the database isn't updated in real-time—only after running the updatedb
command.
-
Install
mlocate
: To install the package:yum install mlocate
-
Update Database: The
updatedb
command updates the database of files, making thelocate
command faster by indexing all files:updatedb
-
Locating Files Using
locate
: After the database is updated, you can uselocate
to search for files quickly.Example commands:
-
Count Entries in Database: To check how many entries are in the
mlocate
database:locate -S
-
Display Database File Location: To show where the
mlocate
database is stored:cat /var/lib/mlocate/mlocate.db
-
Search for a File: Search for files or directories by name. For example, to find files related to "messages":
locate messages
-
Case-insensitive Search: To search for files without worrying about case (e.g., "root" or "ROOT"):
locate -i root
-
Use Regular Expressions: You can use regular expressions to find files that match certain patterns. For example, to find all
.log
files:locate -r '\.log$'
-
Count the Number of Results: To count how many files match the search pattern:
locate -c -i *pass*
-
Limit Output: You can limit the number of results returned. For example, to show only the first 20 results for
.html
files:locate "*.html" -n 20
-
Use
sed
for Specific Line Output: To print only lines 10 to 20 of thelocate
output:locate "*.log" | sed -n '10,20p'
-
The which
command is used to locate the executable file for a command (binary file). It shows the path of the binary file that's used when you type the command in the terminal.
- Example Commands:
- Find where the
ls
command is located:which ls
- Find where the
zip
command is located:which zip
- If a command is not found,
which
will show an error:which nmap
- Find where the
The whereis
command searches for the binary, source, and manual pages for a command.
- Example Commands:
- To find the
ls
command:whereis ls
- To find the
zip
command and its manual pages:whereis zip
- To find the
cp
command and its manual pages:whereis cp
- To find the
The diff
command is used to compare two files line by line. It highlights the differences between the files.
-
Basic Usage: To compare two files (
passwd
andpasswd2
):diff passwd passwd2
-
Contextual Comparison: To see which lines are different along with some context (lines before and after):
diff -c passwd passwd2
-
Side-by-Side Comparison: To compare files side by side:
diff -y passwd passwd2
-
Visual Comparison with
vimdiff
: Usevimdiff
for a more visually friendly way of comparing files with color highlights:vimdiff passwd passwd2
-
mlocate
: Use theupdatedb
command to update a file database, and then uselocate
to quickly search for files. This is much faster than searching the filesystem directly. -
which
: Finds the path of executable files for a given command. -
whereis
: Finds the binary, manual pages, and other files related to a command. -
diff
: Compares two files and shows their differences. -
vimdiff
: A visual and interactive way to compare files, useful for viewing differences in a more user-friendly manner.
These tools are essential for locating files quickly, managing files, and comparing file contents effectively in Linux.