Working With Files
Table of contents
Managing Files
1. Linux: Everything Treated as a File
In Linux, everything, including devices, directories, and processes, is treated as a file.
This abstraction allows a consistent interface for interacting with diverse entities in the system.
2. File Creation Using touch
and Timestamp Modification
The
touch
command is used to create empty files or update the access and modification timestamps of existing files.Syntax:
touch filename
Multiple Files Creation:
touch file1 file2 file3
Change Timestamp:
touch -c -t
YYYYMMDDHHMM.SS
filename
touch
: The command itself, is used for file timestamp manipulation.-c
(or--no-create
): This option prevents the creation of new files. If the specified file does not exist,touch
will not create it.-t
YYYYMMDDHHMM.SS
: This option allows you to specify a specific timestamp for the file in the format[[CC]YY]MMDDhhmm[.SS]
. The components of the timestamp are as follows:CC
: First two digits of the year (the century).YY
: Last two digits of the year.MM
: Month (01-12).DD
: Day of the month (01-31).hh
: Hour (00-23).mm
: Minute (00-59)..SS
: Optional seconds (00-59).
filename
: The name of the file you want to create or modify.
3. File Types
File permissions are represented by a series of characters like
drwxr-xr-x
. In This we can identify various types of files.Regular File (
-
):Represented by a hyphen (
-
).It is a standard file containing data (text, binary, etc.).
Example:
-rw-r--r--
.
Directory (
d
):Represented by the letter
d
.It is a container for other files and directories.
Example:
drwxr-xr-x
.
Symbolic Link (
l
):Represented by the letter
l
.It is a reference to another file or directory.
Example:
lrwxrwxrwx
.
Block Device (
b
):Represented by the letter
b
.It is a special file representing a block-oriented device (e.g., hard drive partitions).
Example:
brw-rw----
.
Character Device (
c
):Represented by the letter
c
.It is a special file representing a character-oriented device (e.g., terminals).
Example:
crw-rw----
.
Named Pipe or FIFO (
p
):Represented by the letter
p
.It is a special file used for inter-process communication.
Example:
prw-r--r--
.
Socket (
s
):Represented by the letter
s
.It is a special file used for inter-process communication (IPC) between processes on the same or different hosts.
Example:
srwxr-xr-x
.
Door (
D
):Represented by the letter
D
.It is a special file used for communication between processes on the same host.
Less common and not widely used.
Whiteout (
w
):Represented by the letter
w
.It is a special file used in certain file systems to represent a deleted file.
Rarely seen in modern systems.
Example:
Here is an example showing different file types:
-rw-r--r-- 1 user user 0 Dec 1 10:00 regular_file
drwxr-xr-x 2 user user 4096 Dec 1 10:00 directory
lrwxrwxrwx 1 user user 5 Dec 1 10:00 symbolic_link -> otherfile
brw-rw---- 1 user disk 8, 1 Dec 1 10:00 block_device
crw-rw---- 1 user tty 5, 0 Dec 1 10:00 character_device
prw-r--r-- 1 user user 0 Dec 1 10:00 named_pipe
srwxr-xr-x 1 user user 0 Dec 1 10:00 socket
4. Links
Hard Links: Point to the same inode as the original file. Changes in one reflect in others.
Creating Hard Links:
touch originalfile ln originalfile hardlink1 ln originalfile hardlink2
Now, you have three files:
originalfile
,hardlink1
, andhardlink2
. They are all hard links, sharing the same content.Modifying Content:
echo "Hello from hardlink1" > hardlink1 cat originalfile # Displays "Hello from hardlink1"
Changes made to one hard link (
hardlink1
) are reflected in all other hard links (originalfile
andhardlink2
).Deleting a Hard Link:
rm hardlink2
Deleting one hard link (
hardlink2
) doesn't affect the others (originalfile
andhardlink1
).
Soft Links (Symbolic Links): Point to the filename, not the inode. They are more flexible but can break if the original file is moved.
Creating Symbolic Links:
touch originalfile ln -s originalfile softlink
Now, you have two files:
originalfile
andsoftlink
.softlink
is a symbolic link pointing tooriginalfile
.Modifying Content:
echo "Hello from originalfile" > originalfile cat softlink # Displays "Hello from originalfile"
Changes made to the original file (
originalfile
) are not automatically reflected in the symbolic link (softlink
).Deleting the Original File:
rm originalfile
Deleting the original file doesn't affect the symbolic link, but now
softlink
is a "dangling link" since the target file is gone.Creating a Soft Link to a Directory:
mkdir mydir ln -s mydir dirlink
Now, you have a symbolic link (
dirlink
) pointing to a directory (mydir
).
5. File Command
The
file
command is used to determine the file type.Examples:
file image.jpeg
: Displays image file format.file /dev/sda1
: Identifies a block device.file /proc/cpuinfo
: Reveals it as an empty file, dynamically updated by the kernel.file filename.rpm
: Shows information about a RPM file.file logrotate.conf
: Examines the type of configuration file.
File Contents
1. cat
Command:
Usage:
cat filename
Description:
- Concatenates and displays the content of one or more files.
Example:
cat /etc/passwd
2. head
Command:
Usage:
head [options] filename
Description:
- Displays the first part of a file.
Options:
-n N
: Display the first N lines.
Example:
head -n 10 filename
3. tail
Command:
Usage:
tail [options] filename
Description:
- Displays the last part of a file.
Options:
-n N
: Display the last N lines.
Example:
tail -n 20 filename
4. >
and >>
:
cat > newfile.txt
Usage:Redirects standard input to a file, creating or overwriting the file.
Enter text, and use
Ctrl+D
to stop.
>>
Usage:- Appends text to the end of a file.
Custom Marker (
<<
) Usage:- Allows input until a specified marker is encountered.
Examples:
cat > newfile.txt
cat >> existingfile.txt
cat > newfile.txt << EOL This is a custom marker example. EOL
5. echo
Command:
Usage:
echo [options] [string(s)]
Description:
- Displays a line of text or variable values.
Example:
echo "Hello, World!"
6. tac
Command:
Usage:
tac filename
Description:
- Concatenates and displays the content of a file in reverse order (last line first).
Example:
tac filename
7. more
Command:
Usage:
more filename
Description:
- Displays text one screen at a time, allowing navigation.
Features:
Use the spacebar to scroll down.
Type
/
to search for a specific word.Press
q
to exit.
8. less
Command:
Usage:
less filename
Description:
- Similar to
more
but provides more features and allows both forward and backward navigation.
- Similar to
Features:
Use the spacebar or
f
to scroll down.Use the percentage to see progress.
Press
q
to exit.
9. Viewing Binary Files:
Binary File with
cat
:cat
may not display binary files properly.Use
strings
to extract readable text from binary files.cat /usr/bin/zsh
strings /usr/bin/zsh
Rhel Playground:-
https://www.redhat.com/en/interactive-labs/install-software-using-package-managers
Subscribe to my newsletter
Read articles from Afridi Shaik directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by