DEV Community

Kruti Bhagat
Kruti Bhagat

Posted on

Linux File Handling Commands Cheatsheet

Linux file handling commands cheatsheet for quick revision.

File Handling

Access Files and Directories

Command Description
pwd Print working directory
ls List files and directories
ls -l List files with detailed information
ls -a List all files including hidden files
cd <directory> Change to specified directory
cd .. Move to parent directory
cd ~ Move to home directory

Create Files and Directories

Command Description
touch <filename> Create an empty file
touch <file1> <file2> Multiple file creation
mkdir <directory> Create a new directory
mkdir -p nested/dir Create nested directories

Remove Files and Directories

Command Description
rm <filename> Delete a file
rm <file1> <file2> Delete multiple files
rmdir <directory> Delete an empty directory
rm -r <directory> Delete a directory and its contents
rm -rf <directory> Force delete a directory and its contents

Copy Files and Directories

Command Description
cp <source> <destination> Copy a file
cp -r <source_dir> <destination_dir> Copy a directory recursively

Move and Rename Files and Directories

Command Description
mv <source> <destination> Move a file or directory
mv <oldname> <newname> Rename a file or directory

Read and View Files

Command Description
cat <filename> Display entire file content
less <filename> View file page by page (forward/backward navigation)
more <filename> View file page by page
head <filename> Display first 10 lines
head -n <number> <filename> Display first n lines
tail <filename> Display last 10 lines
tail -n <number> <filename> Display last n lines
tail -f <filename> Monitor file updates in real time

Search Within Files

Command Description
grep "<text>" <filename> Search for text in a file
grep -i "<text>" <filename> Case-insensitive search
grep -r "<text>" <directory> Search recursively in a directory

File Information

Command Description
ls -l Display detailed file information
file <filename> Display file type
stat <filename> Display detailed file metadata
wc <filename> Count lines, words, and characters

Find Files

Command Description
find . -name "<filename>" Find a file by name
find . -name "*.txt" Find all .txt files
which <command> Show location of an executable

Disk Usage

Command Description
du -sh * Display size of files and directories
df -h Display disk usage in human-readable format

View File Permissions

Command Description
ls -l <filename> View file permissions
stat <filename> View detailed permission and ownership information

Change File Permissions (Symbolic)

Command Description
chmod +x <filename> Add execute permission
chmod -w <filename> Remove write permission
chmod u+rw <filename> Give owner read and write permission
chmod g+x <filename> Give group execute permission
chmod o-r <filename> Remove read permission from others
chmod a+r <filename> Give read permission to everyone

Change File Permissions (Numeric)

Command Description
chmod 755 <filename> Owner: rwx, Group: r-x, Others: r-x
chmod 700 <filename> Owner: rwx, Group: ---, Others: ---
chmod 644 <filename> Owner: rw-, Group: r--, Others: r--
chmod 600 <filename> Owner: rw-, Group: ---, Others: ---

Change Ownership

Command Description
sudo chown <user> <filename> Change file owner
sudo chown <user>:<group> <filename> Change owner and group
sudo chgrp <group> <filename> Change group ownership

Vim Editor

Open/Create Files

Command Description
vim <filename> Open or create a file
vim Open Vim without a file

Vim Modes

Key Description
i Insert mode (insert before cursor)
I Insert at beginning of line
a Append after cursor
A Append at end of line
Esc Return to Normal mode

Save and Exit

Command Description
:w Save file
:q Quit Vim
:wq Save and quit
:x Save (if modified) and quit
ZZ Save and quit
:q! Quit without saving

Editing in Vim

Command Description
dd Delete current line
yy Copy (yank) current line
p Paste below cursor
P Paste above cursor
x Delete current character
u Undo last change
Ctrl + r Redo last undone change

Navigation in Vim

Command Description
gg Go to beginning of file
G Go to end of file
:<line_number> Go to a specific line
0 Go to beginning of current line
$ Go to end of current line

Search in Vim

Command Description
/text Search forward for text
?text Search backward for text
n Next search result
N Previous search result

Common Linux Permission Values

Value Permission
7 rwx (Read, Write, Execute)
6 rw- (Read, Write)
5 r-x (Read, Execute)
4 r-- (Read only)
3 -wx (Write, Execute)
2 -w- (Write only)
1 --x (Execute only)
0 --- (No permission)

Top comments (0)