Linux File Management Commands Cheat Sheet

Linux File Management Commands Cheat Sheet 


 # ---------- CREATE FILES ----------


touch file.txt              # create an empty file

> file.txt                  # create or overwrite file instantly

echo "hello" > file.txt     # create file and write text

echo "hello" >> file.txt    # append text to file

cat > file.txt              # create file and type content manually (Ctrl+D to save)

nano file.txt               # create/edit file using nano editor

vim file.txt                # create/edit file using vim editor

gedit file.txt              # create/edit file using GUI editor



# ---------- VIEW FILES ----------


cat file.txt                # display entire file content

less file.txt               # view large file page-by-page (recommended)

more file.txt               # basic paginated view

head file.txt               # show first 10 lines

head -n 20 file.txt         # show first 20 lines

tail file.txt               # show last 10 lines

tail -f file.txt            # watch file live (logs)

nl file.txt                 # show content with line numbers



# ---------- COPY / MOVE / RENAME ----------


cp file1 file2              # copy file

cp -r dir1 dir2             # copy directory recursively

mv old.txt new.txt          # rename file

mv file.txt /tmp/           # move file to another location



# ---------- DELETE ----------


rm file.txt                 # delete file

rm -r dir                   # delete directory and contents

rm -f file.txt              # force delete without prompt

rm -rf dir                  # force delete directory recursively (dangerous)



# ---------- SEARCH FILES ----------


find / -name file.txt       # search entire system for file name

find . -name "*.yml"        # search current directory for yaml files

locate file.txt             # fast search using database

grep "text" file.txt        # search word inside file

grep -r "text" dir/         # search inside all files recursively



# ---------- FILE INFORMATION ----------


ls                          # list files

ls -l                       # detailed list (permissions, owner, size)

ls -a                       # show hidden files

ls -lh                      # human readable sizes

file file.txt               # show file type

stat file.txt               # detailed metadata

du -h file.txt              # disk usage (size)

wc file.txt                 # count lines, words, characters



# ---------- PERMISSIONS / OWNERSHIP ----------


chmod 755 file.txt          # change file permissions

chmod +x script.sh          # make file executable

chown user file.txt         # change owner

chgrp group file.txt        # change group



# ---------- COMPRESS / ARCHIVE ----------

tar -cvf archive.tar dir/   # create tar archive

tar -xvf archive.tar        # extract tar archive

gzip file.txt               # compress file (.gz)

gunzip file.txt.gz          # decompress file

zip file.zip file.txt       # create zip file

unzip file.zip              # extract zip file


# ---------- OTHER USEFUL ----------

truncate -s 0 file.txt      # empty file content without deleting

diff file1 file2            # compare two files

sort file.txt               # sort file lines

uniq file.txt               # remove duplicate lines


Comments

Popular posts from this blog

Managing Amazon EBS Volumes and Snapshots Across Regions

Git for Beginners: Complete Guide from Installation to First Push on GitHub

AWS - Amazon Web Services