Find Files in Linux (Red Hat)
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
- 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.
–type
• 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
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
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
• head and tail
These commands let you look at the lines starting at the top or bottom of the file, respectively.
Text Manipulation
• sort
• wc
This can be used to sort input alphabetically, numerically, or with different fields as the sort key. Sorting is also available in reverse.
Short for “word count,” this utility can count the number of lines, words, characters, bytes, and the length of the longest line in the file.
• diff
Usually used to generate patch files, diff can display the differences between two or more files.
Linking Files
The file name is a pointer to the inode
There are two types –
- hard link – This is a link between files that point to the same inode
- soft link – is a pointer, acts like a redirect
RHCSA MiniSeries
Part 1 – How to use Grep and Regular Expressions (RegEx)
Part 2 – How to manipulate files in RedHat
Part 3 – Red Hat Permissions
Part 4 – How to change the root password on Red Hat
Part 5 – How to use Red Hat as a virtual machine host
Part 6 – How to configure local storage
Part 7 – how to manage users and groups in Red Hat Linux
Part 8 – how to configure NTP Network Time Protocol in Red Hat
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 […]