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)