boucle ∞

Sparking curiosity, facilitating knowledge-sharing, and fostering community interaction.

Mastering File Manipulation
Published 12 july 2023

In any operating system, file manipulation is a fundamental skill. Red Hat Enterprise Linux (RHEL) comes with a vast array of tools that allow users to create, view, modify, and move files. This article will explore those tools and provide examples of how to use them.

Creating Files with TOUCH

The touch command is used to create a new, empty file. It’s as simple as specifying the name of the file you wish to create:

1
touch newfile.txt

This will create a new file named newfile.txt in the current directory. If the file already exists, touch will update the file’s last access and modification timestamps.

Writing to Files with ECHO and REDIRECTION

echo is used to output text or data, but when used with redirection (>, >>), you can write data to a file. Use > to write data to a file, replacing any existing contents:

1
echo "Hello, world!" > hello.txt

And >> to append data to an existing file:

1
echo "Hello, again!" >> hello.txt

Viewing File Contents with CAT

The cat (short for concatenate) command is used to display the contents of a file. It’s as simple as:

1
cat hello.txt

This will print the contents of hello.txt to the terminal.

Checking File Type with FILE

The file command is used to determine the type of a file. This can be helpful when you’ve received a file without an extension. Simply run:

1
file unknownfile

This command will output the type of unknownfile.

Viewing File Details with STAT

The stat command is used to display detailed information about a file, including size, permissions, and timestamps. Run:

1
stat myfile.txt

And you’ll receive a detailed report about myfile.txt.

Moving and Renaming Files with MV

The mv command serves two purposes: moving and renaming files. To move a file, specify the source and destination paths:

1
mv /path/to/source /path/to/destination

To rename a file, you specify the old name and the new name:

1
mv oldname.txt newname.txt

Remember, if you’re renaming, make sure the destination has the same path as the source, only with a new filename.

Conclusion

Mastering file manipulation in RHEL involves a combination of several commands, each serving a different purpose. The touch, echo, cat, file, stat, and mv commands each provide a piece of the puzzle. With these commands under your belt, you’ll be well-equipped to create, view, modify, and move files in any Linux environment. Continue to practice and explore these commands as you navigate your RHEL system. Happy file manipulating!

Lucian

Open source enthousiast.

×