DEV Community

Cover image for 🐧 Linux Basics Part 2: File Operations, Viewing Content & Permissions
Alkesh Baghel
Alkesh Baghel

Posted on

🐧 Linux Basics Part 2: File Operations, Viewing Content & Permissions

Welcome back to our Linux beginner series! In this part, we’ll explore how to work with files, read their contents, and understand Linux file permissions. Let’s dive right in!


πŸ“ 1. File Operations

βž• Create a File: touch

touch file.txt
Enter fullscreen mode Exit fullscreen mode
  • Creates an empty file named file.txt.

πŸ“ Edit a File (Using nano)

nano file.txt
Enter fullscreen mode Exit fullscreen mode
  • Opens a simple text editor in the terminal.
  • Save with CTRL + O, then press Enter.
  • Exit with CTRL + X.

πŸ—‘οΈ Delete a File: rm

rm file.txt
Enter fullscreen mode Exit fullscreen mode
  • Deletes the specified file.
  • ⚠️ Be careful! There's no recycle bin.

πŸ“ Copy a File: cp

cp file1.txt file2.txt
Enter fullscreen mode Exit fullscreen mode
  • Copies file1.txt to file2.txt.

πŸ” Move or Rename a File: mv

mv oldname.txt newname.txt
Enter fullscreen mode Exit fullscreen mode
  • Renames oldname.txt to newname.txt.
  • You can also move it to another folder.

πŸ“– 2. Viewing File Content

πŸ“„ cat – Show file content

cat file.txt
Enter fullscreen mode Exit fullscreen mode
  • Displays the whole file in the terminal.

πŸ” less – View large files

less bigfile.txt
Enter fullscreen mode Exit fullscreen mode
  • Allows scrolling through the file.
  • Use q to quit.

🧠 head and tail – View start/end of a file

head file.txt
tail file.txt
Enter fullscreen mode Exit fullscreen mode
  • head shows the first 10 lines, tail shows the last 10.
  • Add -n to specify number of lines:
  tail -n 20 file.txt
Enter fullscreen mode Exit fullscreen mode

πŸ” 3. File Permissions

Every file and folder has permissions that control who can read, write, or execute them.

➀ See Permissions: ls -l

ls -l
Enter fullscreen mode Exit fullscreen mode

You’ll see something like:

-rw-r--r-- 1 user group  123 Apr 24 08:00 file.txt
Enter fullscreen mode Exit fullscreen mode

Let’s break it down:

  • -rw-r--r-- = permissions
    • r = read
    • w = write
    • x = execute
  • First set is for owner, second for group, third for others.

πŸ› οΈ Change Permissions: chmod

chmod +x script.sh
Enter fullscreen mode Exit fullscreen mode
  • Makes script.sh executable.

πŸ‘€ Change Ownership: chown

sudo chown tejaswini file.txt
Enter fullscreen mode Exit fullscreen mode
  • Changes the file’s owner to tejaswini.

πŸ” Comparison with Windows

Task Linux Command Windows Equivalent
View file cat, less Notepad, type
Create file touch Right-click β†’ New β†’ Text file
Change perm chmod File properties β†’ Security

🎯 Practice Tips:

  • Try making a text file, adding some text with nano, and viewing it with cat and less.
  • Experiment with changing permissions and making a script executable.

Next up in Part 3, we’ll explore process management, network commands, and a bit of package management! Stay tuned.


Top comments (0)