Home Beginner's Guide Top 15 Linux commands for effective file viewing and editing

Top 15 Linux commands for effective file viewing and editing

This guide introduces 15 essential Linux commands for file viewing and editing. Master commands like 'cat', 'less', 'nano', and 'vim' to efficiently navigate, view, and modify files in Linux, enhancing your command line proficiency and overall system management capabilities.

by Arun Kumar
linux commands for file viewing and editing

In the ever-evolving world of Linux, mastering the art of file management is a skill that stands the test of time. Presently, the Linux operating system offers a rich set of commands for file viewing, editing, and manipulation. These commands are fundamental for anyone navigating through the Linux environment, whether you’re a seasoned system administrator, a developer, or just starting your journey.

From basic commands like ls and cat to more advanced ones like vim and awk, this guide is tailored to provide a comprehensive overview. As we dive into each command, we’ll explore not only their functionalities but also practical examples and nuances that make Linux an efficient and powerful operating system.

Linux commands for navigating and viewing files

1. ls – List directory contents

The ls command is the bread and butter of file navigation in Linux. It lists the contents of a directory. You can use ls -l for a detailed list, which shows file permissions, number of links, owner, group, size, and the last-modified date.

$ ls -l
total 4
drwxr-xr-x 2 user user 4096 Jan 8 09:00 Documents

2. cat – Concatenate and display files

The cat command is great for quickly viewing the contents of smaller files. It’s straightforward – just type cat followed by the file name.

$ cat hello.txt
Hello, FOSS Linux Readers!

3. less – View file contents page by page

When dealing with larger files, less is a lifesaver. It allows you to view the contents of a file one page at a time. Navigate with the arrow keys or q to quit.

$ less largefile.txt

4. head – View the start of files

To quickly peek at the beginning of a file, head is your command. By default, it shows the first 10 lines.

$ head myfile.txt
First 10 lines of myfile.txt...

5. tail – View the end of files

Similar to head, but for the end of files. It’s perfect for checking recent additions, especially in log files. Use tail -f to follow a file in real-time.

$ tail myfile.txt
Last 10 lines of myfile.txt...

Linux commands for editing files

6. nano – Simple text editor

Nano is a beginner-friendly text editor. It’s simple and comes with on-screen instructions. Open a file with nano filename, edit, and use Ctrl + O to save, Ctrl + X to exit.

$ nano myfile.txt

7. vi/vim – Advanced text editors

Vi, and its extended version Vim, are powerful text editors. They have a steep learning curve but offer robust features. Press i to enter insert mode, Esc to return to command mode, :wq to save and exit.

$ vim myfile.txt

8. gedit – GUI-based text editor

Gedit is the default text editor for GNOME. It’s user-friendly and supports multiple languages. It’s handy for those who prefer a graphical interface.

$ gedit myfile.txt &

9. sed – Stream editor for filtering and transforming text

Sed is a stream editor for modifying files in a scriptable way. It’s powerful for pattern-based editing.

$ sed 's/original/replacement/' file.txt

10. awk – Pattern scanning and processing language

Awk is perfect for text processing and data extraction. It’s a bit complex but incredibly powerful.

$ awk '{print $1}' file.txt

Linux commands for file manipulation

11. cp – Copy files and directories

Use cp to copy files or directories. It’s straightforward but be careful with its options to avoid accidental overwrites.

$ cp source.txt destination.txt

12. mv – Move or rename files

Mv serves dual purposes – moving and renaming files. It’s simple but powerful. Just remember, moving files across different filesystems can be slower.

$ mv oldname.txt newname.txt

13. rm – Remove files or directories

Rm is a command you should use with caution – it deletes files permanently. Always double-check the files you are deleting.

$ rm unwantedfile.txt

14. touch – Create or update files

Touch is great for quickly creating an empty file or updating a file’s timestamp. It’s one of those nifty little commands that’s surprisingly useful.

$ touch newfile.txt

15. chmod – Change file permissions

Understanding file permissions is crucial in Linux, and chmod is the command to change them. Be careful with it to avoid unintentional permission changes.

$ chmod 755 myfile.txt

The command $ chmod 755 myfile.txt is an instruction in Linux to change the file permissions of myfile.txt. To understand this command, it’s essential to know a bit about how Linux handles file permissions.

In Linux, file permissions are based on three types of access:

  1. Read (r): The permission to read the contents of the file.
  2. Write (w): The permission to modify or delete the content of the file.
  3. Execute (x): The permission to execute the file, which is particularly relevant for scripts and programs.

These permissions can be set for three different groups:

  1. User (u): The owner of the file.
  2. Group (g): The group that the file belongs to.
  3. Others (o): Everyone else who is not the user or part of the group.

The chmod (change mode) command is used to set these permissions. In the example chmod 755 myfile.txt, the numbers 755 are an octal representation of the permissions being set:

  • 7 for the user: This is calculated as 4 (read) + 2 (write) + 1 (execute) = 7. So, the user (owner) has read, write, and execute permissions.
  • 5 for the group: This is calculated as 4 (read) + 0 (write) + 1 (execute) = 5. The group has read and execute permissions, but not write permissions.
  • 5 for others: The same as for the group – read and execute permissions, but no write permissions.

To visualize it:

  • User: rwx (read, write, execute)
  • Group: r-x (read, no write, execute)
  • Others: r-x (read, no write, execute)

So, when you run $ chmod 755 myfile.txt, you are setting the file permissions so that:

  • The owner can read, write, and execute the file.
  • Members of the group can read and execute, but not modify the file.
  • Everyone else can also read and execute, but not modify the file.

Commands cheat sheet

Navigating and viewing files

Command Description
ls List directory contents
cat Concatenate and display files
less View file contents page by page
head View the start of files
tail View the end of files

Editing files

Command Description
nano Simple text editor
vi/vim Advanced text editors
gedit GUI-based text editor
sed Stream editor for filtering and transforming text
awk Pattern scanning and processing language

File manipulation

Command Description
cp Copy files and directories
mv Move or rename files
rm Remove files or directories
touch Create or update files
chmod Change file permissions

Frequently Asked Questions (FAQs)

I have compiled a list of questions that I found on various forums, which I believe will be helpful for our readers. These questions cover a range of topics and commands, and I encourage you to ask for further clarification if needed. Don’t hesitate to ask more questions, and I’ll do my best to provide the required information.

1. What’s the difference between cat and less?

Cat is typically used for displaying the contents of smaller files directly in the terminal, as it outputs the entire content at once. Less, on the other hand, is more suitable for larger files. It allows you to navigate through the file page by page without loading the entire file content at once, making it more memory-efficient.

2. When should I use vim instead of nano?

Vim is more powerful and suited for complex editing tasks or when you require advanced features like syntax highlighting, search and replace, and custom macros. It’s ideal for experienced users. Nano is simpler and more user-friendly, perfect for quick edits or for beginners who are not familiar with Vim’s unique modes and commands.

3. How can I safely remove files to avoid accidental deletion?

Before using rm, especially with the recursive -r option, double-check the file names. You might also consider using rm -i, which prompts for confirmation before each deletion. For a safer approach, move files to a ‘trash’ directory before permanent deletion, allowing a grace period for recovery.

4. Why would I need to change file permissions with chmod?

File permissions control who can read, write, or execute the file. Adjusting these permissions is crucial for maintaining system security and proper functionality. For instance, scripts need execute permissions to run, and sensitive files might need restricted access.

5. Can awk and sed be used interchangeably?

While both are powerful text processing tools, they serve different purposes. Sed is ideal for simple text substitutions and deletions. Awk is more of a complete programming language and is better suited for complex tasks that involve pattern scanning, processing fields, and producing formatted reports.

6. How do I view hidden files with ls?

To view hidden files (those starting with a dot .), use ls -a. This command lists all files, including hidden ones, in the directory.

7. Is it possible to undo a cp or mv command?

Once a cp or mv command is executed, it cannot be undone through the command line. It’s important to double-check the command before executing it, especially when moving files, as it can overwrite existing files.

8. What’s the easiest way to edit a file for a complete Linux beginner?

For beginners, gedit is an excellent choice because of its intuitive graphical user interface (GUI). It functions similarly to text editors in other operating systems, making it easy for newcomers to start editing files in Linux.

Final thoughts

In this guide, we’ve explored a variety of commands, each serving a unique purpose in the realm of file viewing, editing, and manipulation. Understanding these commands is more than just learning syntax; it’s about grasping the flexibility and power at your fingertips when working in a Linux environment. From the simplicity of ls and cat to the complexity of vim and awk, we covered tools that cater to users of all skill levels.

Mastering these commands is like learning the alphabet of Linux file management. Each has its unique place and utility. My personal favorites? vim for its sheer power and less for its simplicity in viewing large files. I encourage you to practice these commands and discover their full potential.

You may also like

Leave a Comment



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX



“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.