DEV Community

Cover image for Linux Fundamentals (1)
Ajayi wemimo
Ajayi wemimo

Posted on

Linux Fundamentals (1)

How to quit a Vim file.

To quit a file in Vim, you can use a few different commands depending on what you want to do. Here’s a quick guide:

  • Save and Quit:

Press Esc to ensure you are in Normal mode.
Type :wq and press Enter. This saves any changes you’ve made and then quits Vim.

  • Quit Without Saving:

Press Esc to switch to Normal mode.
Type :q! and press Enter. This will quit without saving any changes.

  • Save (if needed) and Quit:

Press Esc to go to Normal mode.
Type :x and press Enter. This saves changes if there are any and then quits. It's similar to :wq but slightly more concise.

  • Quit (only if no changes were made):

Press Esc to ensure you’re in Normal mode.
Type :q and press Enter. This will quit Vim only if no changes have been made. If there are unsaved changes, Vim will warn you.

How to list the content of a directory.

To list the contents of a directory, you can use different commands depending on the operating system you're working with. Here’s how to do it on various platforms:

On Unix-like Systems (Linux, macOS):

  • Basic Listing:

Open a terminal and type ls followed by Enter. This will list the files and directories in the current directory.

  • Detailed Listing:

For a more detailed view including file permissions, ownership, and sizes, type ls -l and press Enter.

  • Including Hidden Files:

To include hidden files (those starting with a dot), use ls -a.
Detailed and Hidden Files:

Combine the options with ls -la or ls -al.

  • Human-Readable Sizes:

For file sizes in a human-readable format (e.g., KB, MB), use ls -lh.
On Windows:

  • Basic Listing:

Open Command Prompt or PowerShell and type dir followed by Enter. This will display the files and directories in the current directory.

  • Detailed Listing:

For a more detailed view, you can use dir with additional switches like /q to display file ownership.
List Specific Files:

You can use dir *.txt to list only files with a specific extension, like .txt.

For Example

Image description

what command can be used to delete the content of directory.

To delete the contents of a directory, you need to use commands specific to your operating system. Here's how to do it on Unix-like systems (Linux, macOS) and Windows:

On Unix-like Systems (Linux, macOS):

  • Delete All Files in a Directory (but not subdirectories):

Image description

This command removes all files in the specified directory but leaves subdirectories intact.

  • Delete All Files and Subdirectories:

Image description

The '-r' (recursive) option removes all files and subdirectories inside the specified directory. Note that this command doesn’t delete the directory itself; it only clears its contents.

  • Delete All Files and Subdirectories, Including Hidden Files:

Image description

This command includes hidden files and directories (those starting with a dot). Be cautious with this command, as it will also attempt to delete '.'and '..', which are special directory entries for the current and parent directories. Most shells handle this safely, but it’s good to be careful.

  • Force Delete Without Confirmation:

Image description

The -f (force) option tells rm to ignore nonexistent files and never prompt for confirmation. This command will delete everything in the directory and the directory itself. Use with caution!

On Windows

  • Delete All Files in a Directory (but not subdirectories):

Image description

The /q (quiet) option avoids prompting for confirmation.

  • Delete All Files and Subdirectories:

Image description

The '/s' option removes all directories and their contents, and the /q option suppresses confirmation prompts. This command deletes the directory itself along with its contents.

  • Using PowerShell:

Delete All Files and Subdirectories

Image description

The '-Recurse' option ensures all subdirectories and their contents are removed, and '-'Force ensures it doesn’t prompt for confirmation.

What command can be used to switch from a normal user to a root user?

To switch from a normal user to the root user, you can use different commands depending on the operating system and the available tools. Here’s how you can do it on Unix-like systems (Linux, macOS):

On Unix-like Systems (Linux, macOS):

  • Using su Command:

Switch to Root User:

Image description

You’ll be prompted to enter the root password. The - (or --login) option ensures that you get a login shell with the root user’s environment.

Switch to Root User Without Changing Environment:

Image description

This command also prompts for the root password but does not change to the root user’s environment.

- Using sudo Command:

  • Switch to Root User:

Image description

This command runs a login shell as the root user. You’ll need to enter your own password (not the root password), assuming your user has the necessary permissions configured in /etc/sudoers.

  • Execute a Single Command as Root:

Image description
This runs with root privileges. For example:

Image description

  • Switch to Root User with a Different Shell:

Image description

This starts a non-login shell as root, so it doesn't fully replicate the root user’s environment like sudo -i does.

On macOS:

Using sudo (macOS does not support su for switching to root directly, but sudo is the preferred method):

Image description
or
Image description
Again, you will need to enter your own password.

Top comments (0)