Skip to main content
Chadura
Linux 28 min read •

The Art of File Handling on Linux

Linux filesystem

Sridhar S
Sridhar S
Cloud Admin
The Art of File Handling on Linux
01

Overview

The Linux file system is a hierarchical structure that organizes and stores data on a computer. It follows a tree-like structure with a single root directory ("/") at the top, branching out into subdirectories and files. Linux uses various file systems such as ext4, XFS, Btrfs, and others to manage how data is stored and accessed. Files are treated as a series of bytes and are organized into blocks for efficient storage. 

02

File Types in Linux

Linux supports several file types:

  • Regular files : Text, binary, or executable files.
  • Directories : Folders that contain other files and directories.
  • Symbolic Links : Shortcuts or pointers to other files.
  • Device files : Represent hardware devices ( e.g., /dev/sda for a hard drive).
  • Pipes and Sockets : Used for inter-process communication.
  • Creating and Editing Files

touch : Creates an empty file or updates the timestamp of an existing file.

Code / Terminal
touch newfile.txt

nano, vim, or gedit (graphical editor ) : Text editors used to create or edit files.

Code / Terminal
nano editor
nano filename.txt
                                                                                                                                               
The Art of File Handling on Linux
Code / Terminal
vim editor 

vim filename.txt
The Art of File Handling on Linux

echo and print: Write text to a file.

Code / Terminal
Writes "Hello World" to file.txt.

echo "Hello World" > file.tx  

printf "Hello World\
" > file.txt
The Art of File Handling on Linux

cp: Copies files or directories.

Code / Terminal
cp source.txt destination.txt 

cp -r : Copying files from Directories

Code / Terminal
cp -r sourcedir/ targetdir/ 

mv : Moves or renames files and directories

Code / Terminal
mv file.txt newfile.txt
  • Renames file.txt to newfile.txt.
Code / Terminal
mv file.txt /path/to/destination/

rm : Deletes files.

Code / Terminal
rm filename.txt
  • Recursively deletes a directory and its contents.
Code / Terminal
rm -r directory/     
  • Prompts for confirmation before deleting the file
Code / Terminal
rm -i filename.txt 
03

File Permissions

Every file and directory in Linux has three permission levels: Owner, Group, and Others, each with three types of access:

Read (r) : View the contents of the file.

Write (w): Modify the file.

Execute (x): Run the file if it’s a script or program.

ls -l : Shows file permissions in the format drwxr-xr-x. 

Here:

  • d indicates a directory.
  • The first three characters (rwx) indicate the owner’s permissions.
  • The next three (r-x) indicate the group’s permissions.
  • The final three (r-x) indicate others' permissions.

chmod : Changes file permissions.

  • Sets read, write, and execute permissions for the owner and read/execute for others (755).
Bash / Terminal
chmod 755 filename.txt
  • Adds execute permissions for the owner(user)
Bash / Terminal
chmod u+x filename.sh 

chown : Changes the ownership of a file or directory.

Bash / Terminal
chown user group filename.txt 
04

File Compression and Archiving

tar : Archives multiple files into a single file.

Code / Terminal
tar -cvf archive.tar <input_directory>
  • Extracts the contents of the archive
Code / Terminal
tar -xvf archive.tar 
  • gzip and gunzip Compresses or decompresses files.
Code / Terminal
gzip file.txt
  • Compresses file.txt to file.txt.gz.
Code / Terminal
gunzip file.txt.gz

zip archive.zip file1 file2
  • Decompresses files in zip format.
Code / Terminal
unzip archive.zip 
05

Advanced File Handling Commands

  • File Searching

find : Searches for files based on criteria (name, size, modification time, etc.).

  • Finds all .txt files in the given path
Code / Terminal
find /path -name "*.txt"   
  • Finds files larger than 1 MB
Code / Terminal
find /path -type f -size +1M  

locate: Quickly finds files using a pre-built database.

Code / Terminal
locate filename

grep: Searches for text patterns within files.

Bash / Terminal
grep "pattern" filename.txt
  • Searches for "pattern" in filename.txt.
Bash / Terminal
grep -r "pattern" /path/ 
06

File Linking

ln :Creates hard or symbolic links.

Hard link: Another name for a file, both references point to the same data on disk.

Code / Terminal
ln file1 file2 

Symbolic link (symlink): A shortcut or pointer to the original file.

Code / Terminal
ln -s file1 link_to_file   
  • File Backup and Synchronization

rsync: Synchronizes files and directories between two locations. rsync is commonly used for backups or mirroring directories.

Code / Terminal
rsyncrsync -avz /source/ /destination/
  • Disk Usage and File Size

du : Shows disk usage of files and directories.

  • Displays the size of a directory in a human-readable format
Code / Terminal
du -sh directory/

The Art of File Handling on Linux
  • Shows the size of files and directories up to a depth of one level.
Code / Terminal
du -h --max-depth=1 df
The Art of File Handling on Linux

df -h:Displays disk space usage in human-readable format

  • File System Navigation

pwd: Prints the current working directory.

The Art of File Handling on Linux

cd: Changes the current directory.

Code / Terminal
cd /path/to/directory
The Art of File Handling on Linux

cd.. : Moves to the specified directory.

cd ~ : Moves to the home directory.

Sridhar S

Sridhar S

Cloud Admin

Cloud Admin - Chadura Tech Pvt Ltd, Bengaluru

Related Technical Articles

Explore additional architectural breakdowns in Linux.

M

Maya Solutions Architect

Ask about products & scoping →

WhatsApp Direct Help