Linux File Operations Commands
In this article
Linux file operations commands are the command-line tools used to create, view, copy, move, rename, and delete files and directories. This article covers the essential commands every user needs, along with practical examples and common pitfalls.
Background: Why Command Line?
Linux is built around the philosophy that everything is a file. The command line gives you precise control over these files, often faster and more flexibly than a graphical file manager. Commands like ls, cp, mv, and rm have existed since the early days of Unix and remain unchanged in their core functionality. Learning them is essential for server administration, scripting, and automation.
Listing and Navigating Files
The ls command lists directory contents. Basic usage:
ls– shows files and directories in the current directory.ls -l– long format with permissions, owner, size, and modification date.ls -a– includes hidden files (those starting with a dot).ls -lh– human-readable sizes (KB, MB, GB).
To change directories, use cd (e.g., cd /var/log). The pwd command prints the current working directory.
Creating and Viewing Files
To create an empty file, use touch:
touch newfile.txtTo view file contents, common commands are:
cat file.txt– prints entire file to the terminal.less file.txt– allows scrolling through large files (pressqto quit).head file.txt– shows first 10 lines.tail file.txt– shows last 10 lines.
For editing, use text editors like nano or vim.
Copying, Moving, and Renaming
The cp command copies files or directories:
cp source.txt destination.txtTo copy a directory recursively, use -r:
cp -r folder/ backup_folder/The mv command moves or renames files:
mv oldname.txt newname.txtIt works the same for directories. Unlike cp, mv does not require -r for directories.
Deleting Files and Directories
The rm command deletes files:
rm file.txtTo delete a directory and its contents recursively, use -r:
rm -r folder/Warning: Deletion is permanent. There is no trash bin in the command line. Use rm -i to be prompted before each deletion. Be extremely careful with rm -rf – it can wipe entire filesystems if used incorrectly.
Permissions and Ownership
Every file has an owner and permissions for owner, group, and others. Use chmod to change permissions:
chmod 755 script.shThis gives the owner read/write/execute (7), group read/execute (5), and others read/execute (5). Use chown to change ownership:
chown user:group file.txtView permissions with ls -l. For example, -rw-r--r-- means owner can read/write, group and others can only read.
Finding and Searching Files
The find command searches for files by name, type, size, or modification time:
find /home -name "*.txt"This finds all .txt files under /home. The grep command searches inside file contents:
grep "error" logfile.logTo search for a file by name faster, use locate (if installed), but it relies on a database that may be outdated.
Common Problems and Solutions
- Permission denied – You lack write permission. Use
sudoonly if you understand the risk, or check ownership withls -l. - File not found – Check your current path with
pwdand uselsto verify the name. - Accidental deletion – There is no built-in undo. Consider using a version control system like Git for important files.
- Special characters in filenames – Use quotes or escape with backslash:
rm "my file.txt".
Frequently Asked Questions
How do I copy a file but keep the same name?
Use cp source.txt /path/to/destination/. The file will keep its original name in the destination directory.
What is the difference between cp and mv?
cp creates a duplicate, leaving the original intact. mv moves the file, removing it from the original location. mv is also used for renaming.
How do I delete a directory that is not empty?
Use rm -r directory_name. The -r flag recursively deletes all contents. Be absolutely sure you want to delete it, as this cannot be undone.
How do I view hidden files?
Use ls -a. Hidden files start with a dot (e.g., .bashrc). To view them in a file manager, enable "Show Hidden Files" in the view options.
What does chmod 777 do?
It gives read, write, and execute permissions to everyone (owner, group, and others). This is generally unsafe and should be avoided except for specific cases like temporary shared files.
Can I undo a rm command?
No, rm permanently deletes files. There is no built-in undo. Some filesystems or backup tools may recover data, but it is not guaranteed. Always back up important files.