Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Linux File Operations Commands

· 4 min read · Home Tech

In this article
  1. Background: Why Command Line?
  2. Listing and Navigating Files
  3. Creating and Viewing Files
  4. Copying, Moving, and Renaming
  5. Deleting Files and Directories
  6. Permissions and Ownership
  7. Finding and Searching Files
  8. Common Problems and Solutions
  9. Frequently Asked Questions

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.txt

To view file contents, common commands are:

  • cat file.txt – prints entire file to the terminal.
  • less file.txt – allows scrolling through large files (press q to 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.txt

To copy a directory recursively, use -r:

cp -r folder/ backup_folder/

The mv command moves or renames files:

mv oldname.txt newname.txt

It 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.txt

To 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.sh

This 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.txt

View 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.log

To 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 sudo only if you understand the risk, or check ownership with ls -l.
  • File not found – Check your current path with pwd and use ls to 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.