wq Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

In the world of Linux, the 'wq' command plays a vital role when it comes to handling files in vi or vim text editors. The 'wq' command in Linux is specifically designed for the vi and vim environments, and is used to write (save) the current file and quit the editor simultaneously. If you have ever worked in a vi or vim editor, you know that modifications are not saved automatically - this is where the 'wq' command steps in, providing an efficient way to save your progress and exit the editor.

Syntax of wq Command in Linux

The basic syntax of the wq command in Linux is as follows:

Where:

  • :: The colon signifies the command mode in vi or vim editor.
  • wq: 'wq' is the command to write (save) and quit the editor.
  • !: The exclamation mark is used to force the command to execute, overriding any warnings or prompts.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Options in wq Command in Linux

  1. :wq: Writes the current file and exits the vi or vim editor.

    For example -

    Output:

    The '' command saves the changes made to the file and then closes the vi or vim editor.

  2. :wq!: Forces the current file to be written and quits the editor, even when the file is read-only.

    For example -

    Output:

    The '!' command forcefully saves the changes made to the file, even if it is read-only, and then closes the editor.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example Usages

  • Saving changes to a file and exiting the editor:

    Output:

    Explanation: This is the most common usage of the 'wq' command in Linux, allowing users to save their progress and quit the editor.

  • Forcefully saving changes to a read-only file and exiting the editor:

    Output:

    Explanation: This usage of the 'wq' command in Linux is helpful when you need to save changes to a file that is marked as read-only.

Tips

  • Remember to switch to command mode (by pressing ESC) in vi or vim editor before using the 'wq' command.

  • If you made changes that you don't want to save, you can use '!' to quit without saving.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Advanced Use Cases of wq Command in Linux

  • Saving changes to a file under a new name and exiting the editor:

    Output:

    Explanation: This advanced usage of the 'wq' command in Linux allows you to save the changes under a new filename and quit the editor. This is useful when you want to keep the original file unchanged.

  • Saving changes to all open files and exiting the editor:

    Output:

    Explanation: This usage of the 'wq' command in Linux is useful when you have multiple files open in the editor and want to save changes to all of them at once and quit the editor.

Conclusion

  • The 'wq' command in Linux is used in vi or vim editor to write (save) and quit.

  • The 'wq!' command is used to forcefully save changes and quit, even when the file is read-only.

  • Advanced usage of the 'wq' command allows for saving changes under a new filename and saving changes to multiple files at once.