The Art of Finding Files in Linux
It doesn’t matter if you have worked on Linux your entire life or if you are a complete noob. Being able to find and manage files in Linux is a skill you will use every day.
In this article, You will learn how to:
- Find files
- Read files
- Manipulate test
- Link files
Finding files is a crucial skill needed for any system admin.
Several operator flags are useful for the find command.
Finding Files with the find
Command
The find
command is your go-to tool for locating files in Linux. It offers a wide range of options to narrow your search. Let’s explore some key file types and search criteria:
Common File Types:
-type f
: Regular files (e.g., text documents, configuration files)-type d
: Directories (folders)-type l
: Symbolic links (shortcuts to other files or directories)
• f = regular file
find . -type f –name httpd.conf
• d = directory
find . –type d –name html
• l = symbolic link
find . –type l –name redhat-release
–user
• File is owned by the user (username or UID)
Find the top 5 files.
find -type f -exec du -Sh {} + | sort -rh | head -n 5
Reading Files
cat
Command: Your Multi-Purpose Text Tool
This reads the file and outputs all of it to stdout.
The cat command in Linux is a versatile tool for working with text files. Here are some useful examples:
Displaying File Contents:
cat filename
This command will display the entire content of the specified file on the terminal.
Concatenating Multiple Files:
cat file1 file2 > newfile
This concatenates the content of file1 and file2 and writes the result to newfile.
Appending to a File:
cat file1 >> existingfile
Appends the content of file1 to the end of existingfile.
Numbering Lines:
cat -n filename
Displays the content of the file with line numbers.
Displaying Line Contents Matching a Pattern:
cat filename | grep "pattern"
Displays lines containing the specified pattern using the grep command.
Displaying Non-Printable Characters:
cat -v filename
Displays non-printable characters as visible representations.
Creating a New File with Content from the Terminal:
cat > newfile
Allows you to type content directly into the terminal, and when you press Ctrl+D, it saves the input to newfile.
Viewing Multiple Files with Page Breaks:
cat file1 file2 | more
Displays the content of file1 and file2 with a page break, allowing you to navigate through the content.
Displaying Tabs as ^I:
cat -T filename
Shows tabs in the file as ^I.
Displaying Line Endings:
cat -E filename
Displays lines with a $ symbol at the end, indicating the line endings.
less
and more
: Navigating Large Files
Also known as”pagers” these commands will output the contents of a file to the screen, but allow you to navigate through the file as well”Also known as “pagers” these commands will output the contents of a file to the screen, but allow you to navigate through the file as well
less filename
: View file with navigation (use arrow keys, spacebar, and /
to search).more filename
: View file with basic scrolling (use spacebar for next page, q
to quit).
head
and tail
: The Beginning and the End
head filename
: Displays the first 10 lines of a file (customize with-n
option).tail filename
: Displays the last 10 lines of a file (useful for logs).
Text Manipulation
sort
: Sorts text numerically, alphabetically, or by specific fields.wc
: Counts lines, words, and characters in a file.diff
: Shows differences between two files (commonly used for comparing versions).
Linking Files
Every file in Linux is associated with an “inode” that stores its metadata (permissions, timestamps, etc.). The filename itself is just a pointer to the inode.
Types of Links:
- Hard Link: Creates a new filename that points to the same inode as the original file. Changes to either file affect both.
- Soft Link (Symbolic Link): Creates a new filename that points to the original filename (not the inode). If the original file is deleted, the soft link becomes broken.
That’s it. Thanks for reading this article. We welcome all feedback in the comments section below.
7 Responses
[…] Part 2 – How to manipulate files in RedHat […]
[…] Part 2 – How to manipulate files in RedHat […]
[…] Part 2 – How to manipulate files in RedHat […]
[…] Part 2 – How to manipulate files in RedHat […]
[…] Part 2 – How to manipulate files in RedHat […]
[…] Part 2 – How to manipulate files in RedHat […]
[…] Part 2 – How to manipulate files in RedHat […]