Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published atyuscode.hashnode.dev

Chapter 2 - Basic Linux Commands

Image description

Introduction

Linux commands form the backbone of working with a Linux system. Mastering these commands allows you to efficiently navigate and manipulate files, directories, and system resources. In this chapter, we will cover the most essential commands for file and directory operations.

Table of Contents

Commands for File and Directory Operations

ls

Thels command is used to list files and directories within the file system.

ls
Enter fullscreen modeExit fullscreen mode
  • ls -/: Lists all files and directories at the root of the file system.

    ls /
  • ls -l: Provides a detailed listing including file permissions, number of links, owner, group, size, and timestamp.

    ls-l
  • ls -a: Shows all files, including hidden files.

    ls-a

cd

Thecd command is used to change the current directory.

cd[directory]
Enter fullscreen modeExit fullscreen mode
  • cd ~: Changes to the current user's home directory.

    cd ~
  • cd ..: Moves up one directory level.

    cd ..

pwd

pwd stands for "print working directory". This command displays the current directory.

pwd
Enter fullscreen modeExit fullscreen mode

touch

Thetouch command is used to create an empty file or update the timestamp of an existing file.

touchfile1.txt
Enter fullscreen modeExit fullscreen mode

cat

Thecat command is used to view the contents of a file.

cat[file_name]
Enter fullscreen modeExit fullscreen mode
  • cat file1.txt : Displays the content of file1.txt.

    catfile1.txt

mkdir

Themkdir command is used to create new directories.

mkdir[directory_name]
Enter fullscreen modeExit fullscreen mode
  • To create a nested folder structure in Linux, you can use the mkdir command with the-p option like this:

    mkdir-p directory-1/directory-2/directory-3/directory-4

    The-p option tellsmkdir to create the parent directories as needed.

rmdir

Thermdir command is used to remove empty directories.

rmdir[directory_name]
Enter fullscreen modeExit fullscreen mode

cp

The cp command is used to copy files or directories.

cp[source][destination]
Enter fullscreen modeExit fullscreen mode
  • cp file1.txt /home/user: Copiesfile1.txt to the/home/user directory.

    cpfile1.txt /home/user
  • cp -r dir1 /home/user : Recursively copies the directorydir1 and its contents to/home/user. Recursively as used in this case means to copy everything in the specified directory (dir1), including all its contents, subdirectories, and sub-subdirectories, and so on, all the way down.

    cp-r dir1 /home/user

mv

Themv command is used to move or rename files or directories.

mv[source][destination]
Enter fullscreen modeExit fullscreen mode
  • mv file1.txt /home/user : Movesfile1.txt to the/home/user directory.

    mvfile1.txt /home/user
  • mvold_name.txtnew_name.txt : Renamesold_name.txt tonew_name.txt.

    mvold_name.txt new_name.txt

rm

Therm command is used to remove files or directories.

rm[file_name]
Enter fullscreen modeExit fullscreen mode
  • rm file1.txt : Deletesfile1.txt.

    rmfile1.txt
  • rm -r dir1 : Recursively deletes the directorydir1 and its contents.

    rm-r dir1

Globbing

Globbing is a way to find files on your computer that match a certain pattern. Think of it like a search function, but instead of searching for a specific word or phrase, you can use special characters called "wildcards" to match many files at once. Globbing is useful because it saves you time and effort when working with files. You can use globbing to:

  • Find and delete many files at once

  • Copy or move many files to a new location

  • Run scripts or programs on many files at once

Wildcards

In globbing, "wildcards" are special characters that represent one or more characters in a file name. Here are the most common wildcards:

  • * (asterisk): matches any characters (including none).

  • [abc] (square brackets): matches any character inside the brackets (e.g., a, b, or c).

  • [a-z] (square brackets with a range): matches any character in the range (e.g., a to z).

Below are example codes for each of the wildcards listed above:

  • * (asterisk):

    ls*.txt

    The command above lists all files ending with .txt

  • [abc] (square brackets):

    ls[ab]*.txt

    The command above lists files starting with a or b and ending with .txt (e.g., a_file.txt, b_file.txt)

  • [a-z] (square brackets with a range):

    ls[a-z]*.txt

    The command above lists files starting with any lowercase letter and ending with .txt (e.g., a_file.txt, z_file.txt)

    NB: These examples use thels command to list files, but you can replacels with other commands likerm,cp, ormv to perform different actions on the matched files.

Conclusion

Mastering basic Linux commands is essential for navigating and managing a Linux system effectively. These commands form the foundation of working with files, directories, and system resources, empowering you to perform various tasks efficiently.

Feel free to leave comments and share this article. Follow my blog for more insights on Linux!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hello. My name is Yusuf Isah, and I am a DevOps enthusiast from Nigeria. I am also passionate about Technical Writing.
  • Joined

More fromYusuf Isah

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp