This guide contains key commands for managing files and directories in Linux, which will be useful for every DevOps specialist.
Working with Files and Directories
- ls -lah — Display a list of files with detailed information
- cd /path — Navigate to the specified directory
- pwd — Show the full path to the current directory
- mkdir new_dir — Create a new folder
- rm -rf file/dir — Completely delete a file or directory
- cp file1 file2 — Copy a file or folder
- mv file1 file2 — Move or rename a file
- touch file.txt — Create a new empty file
- cat file — Display the contents of a file in the terminal
- tail -f file — Show new lines of a file in real-time
Viewing and Editing Files
- less file — View a file page by page
- head -n 10 file — Display the first 10 lines of a file
- tail -n 10 file — Display the last 10 lines of a file
- nano file — Open a file in the Nano editor
- vim file — Open a file in the Vim editor
- grep "error" file — Find a line containing "error" in a file
Managing Permissions and Ownership
- chmod 755 file — Set file permissions (rwxr-xr-x)
- chown user:group file — Change file owner
- umask 022 — Set file creation mask
Archiving and Compression
- tar -cvf archive.tar folder/ — Create a tar archive
- tar -xvf archive.tar — Extract a tar archive
- tar -czvf archive.tar.gz folder/ — Create a compressed tar.gz archive
- tar -xzvf archive.tar.gz — Extract a tar.gz archive
- zip -r archive.zip folder/ — Create a zip archive
- unzip archive.zip — Extract a zip archive
Working with Symbolic Links
- ln -s /path/to/target link_name — Create a symbolic link
- ls -l link_name — Check the link
- readlink -f link_name — Find out where the link points
Disk Usage Monitoring
- df -h — Check available disk space
- du -sh file/ — Get the size of a file or folder
- du -h --max-depth=1 /path — Get the size of folders in the specified directory
Useful Tricks
- history | grep command — Find a command in history
- !! — Repeat the last command
- !n — Execute the nth command from history
- df -h | grep "/dev/sd" — Show only physical disks
- find /path -name "*.log" — Find files with the .log extension
FAQ (Frequently Asked Questions)
Can deleted files be recovered after rm -rf?
Usually, no. The rm -rf command permanently deletes files. However, if the filesystem supports a "trash bin" (e.g., via the trash-cli utility), safe deletion can be configured.
How do I copy a folder along with its contents?
Use cp -r source_dir destination_dir, where the -r flag enables recursive copying.
How can I quickly find a file on the system?
Use find / -name "filename" to search the entire system or locate filename if the locate utility is installed.
Conclusion
This quick reference guide contains essential commands for managing files and directories in Linux. Knowing these commands will streamline system management and make daily tasks easier for DevOps engineers.