0% found this document useful (0 votes)
23 views12 pages

Essential Linux File Operations Guide

The document provides a comprehensive guide on various file operations in a Linux environment, including commands for creating, moving, renaming, and removing files and directories. It also covers file listing, searching with grep, and I/O redirection techniques. Additionally, it explains the types of files and the differences between soft and hard links.

Uploaded by

rohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

Essential Linux File Operations Guide

The document provides a comprehensive guide on various file operations in a Linux environment, including commands for creating, moving, renaming, and removing files and directories. It also covers file listing, searching with grep, and I/O redirection techniques. Additionally, it explains the types of files and the differences between soft and hard links.

Uploaded by

rohit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FILE OPERATIONS

cat – Concatenate & Display Files


Create a new file
# cat > [Link]
Hello World
Ctrl+d # Save and exit
Append to a file
cat >> [Link]
Display contents
cat [Link]
Display contents in reverse Order
tac [Link]
Concatenate multiple files
cat file1 file2 > [Link]
Number lines
cat -n [Link]
Suppress repeated empty lines
cat -s [Link]

Input redirection
cat < [Link] ### usefile while updating the passwords for bulk users.
#chpasswd < [Link]

Here‑document --- To insert the Config files content on the go during


script executions.
cat <<EOF > [Link]
Line 1
Line 2
EOF

✨ touch – Create & Update Files


Create empty file
touch file1
Create multiple files
touch file1 file2 file3
Bulk create with brace expansion
touch {1..100} # Files 1 to 100
touch log{001..100}.txt # [Link] … [Link]
Update timestamps
touch existingfile
Set specific timestamp
touch -t 202511172230 file1
Change only access time
touch -a file1
Change only modification time
touch -m file1
Use reference file’s timestamp
touch -r [Link] [Link]

📁 cd – Change Directory
Basic navigation
cd /path/to/dir # Absolute path
cd projects/linux # Relative path
cd # Home directory
cd - # Previous directory
Absolute path → Always starts with /, independent of current location.

Relative path → Based on current directory, uses . (current) and ..


(parent).

Logical (-L) → Follow symlinks (default).


cd -L symlink_dir
Physical (-P) → Resolve actual directory, ignore symlinks.
cd -P symlink_dir

📂 mkdir – Make Directories


Create a single directory
mkdir mydir
Create nested directories
mkdir -p parent/child/grandchild
Create multiple directories
mkdir dir1 dir2 dir3
Complex nested structure
mkdir -p
KernelTech/{Linux/{advlinux,linuxclstr},Aix/{hacmp,lpar},Storage/{san,n
etapp}}

mv – Move & Rename


Move a file
mv file1 dir1/
Move a directory
mv dir1 dir2/
Rename a file
mv oldfile newfile
Rename a directory
mv olddir newdir

rm / rmdir – Remove Files & Directories


Remove a single file
rm file1
Remove multiple files
rm file1 file2 file3
rm *.log
rm file{1..10}
Force remove (no prompt)
rm -f file1
Interactive remove (confirm each)
rm -i file1
Remove an empty directory
rmdir dir1
Remove a directory and its contents
rm -r dir1
rm -rf dir1 # Dangerous: no confirmation
ls - List file names
ls -l - Long listing (permissions, owner, size, date)
ls -l filename - Show details of a specific file
ls -ld directory name - - Show details of a specific directory.
ls -al - Show all files including hidden, sorted by modification time
ls -lrt - Lists in the order of their creation time
Pattern matching(globbbing)
ls p* # Files starting with 'p'
ls ?ample # Any first character + 'ample'
ls [ae]* # Files starting with 'a' or 'e'
ls [!ae]* # Files NOT starting with 'a' or 'e'
ls [a-m][c-z][4-9] # Files matching ranges
Directory details
ls -ld l* # Show directory info only
ls -ld dirname # Permissions of a specific directory
stat
The stat command in Linux is used to display detailed information
(metadata) about files and file systems, including size, permissions,
ownership, and timestamps. It provides more detail than ls -l, since it
shows inode-level data.
# stat filename
Displays metadata of the file, such as:
File name
Size (in bytes)
Blocks used
IO Block size
File type (regular file, directory, etc.)
Device & Inode number
Links (hard link count)
Access permissions (numeric and symbolic)
Owner (UID) and Group (GID)
Timestamps:
Access (last read)
Modify (last content change)
Change (last metadata change)
Birth (creation time, if supported)

stat [Link] [Link] # For multiple files


stat -c %s filename # Show only file size
stat -c %n filename # Show only file name
stat -c %y filename # Show last modification time

Types of Files
ls -la --- use this command to check the type of file.
- → Normal file
d → Directory
l → Link file (shortcut)
b → Block device (hard disk, floppy)
c → Character device (keyboard, mouse)

🔗 Links (Soft vs Hard)


Create soft link
ln -s source_file destination
Create hard link
ln source_file destination

🔍 Grep (Global Regular Expression Print)


Search for a word
grep root /etc/passwd
Case-insensitive search
grep -i kernel ktfile
Show match with context
grep -nA2 wheel /etc/group # 2 lines after
grep -nB2 wheel /etc/group # 2 lines before
Exclude matches
grep -v kernel ktfile
Highlight matches
grep --color root /etc/passwd
Combine with other commands (pipes)
cat filname | grep -i string
cat ktfile | grep -i kernel
ls -l | grep -i ktfile
ifconfig | grep -i eth0

🪄 Filter Commands
less → View file page by page
less /etc/passwd
Enter → scroll line by line
d → next page
b → previous page
/word → search
v → edit in vi mode

more → Similar to less


more /etc/passwd
head → Show first 10 lines

head /etc/passwd
head -n 20 /etc/passwd # Custom number of lines
tail → Show last 10 lines

tail /etc/passwd
tail -n 20 /etc/passwd # Custom number of lines
tail -f filename # lists the live logs of a file.

sort → Sort alphabetically or numerically


sort filename
sort -d ktfile # Dictionary order
sort -h ktfile # Human-readable numbers
sort -u ktfile # Remove duplicates

cut → Extract columns/fields


cut -d " " -f1 filename # Space delimiter
cut -d, -f1 filename # Comma delimiter
sed → Stream editor (search & replace)
sed 's/searchfor/replacewith/g' filename

🔀 I/O Redirection
Redirect output to a file
cat oldfile > newfile # Overwrite
cat oldfile >> newfile # Append
cat file1 file2 > >file3 # Apending the contents of multiple files.
Using tee (save + display)
cat ktfile | tee newfile
cat ktfile | tee -a existingfile # Append

✅ Quick Summary
cat → Work with file contents (create, view, append, merge).
touch → Create empty files, update timestamps, bulk file creation.

cd → Navigate directories (absolute, relative, symlink handling).

mkdir → Create directories (single, nested, multiple).

mv → Move or rename files and directories.

rm / rmdir → Remove files and directories (with caution).


Basic listing

ls → List files with patterns and permissions.

File types → Normal, directory, link, block, character.

Links → Soft vs hard links (different inode behavior).

grep → Search text with regex, context, and color.

Filters → less, more, head, tail, sort, cut, sed.

Redirection → >, >>, and tee for output control.


-0-0-0-0-

You might also like