Skip to main content

Command Palette

Search for a command to run...

Basic Shell Commands in Linux

Updated
3 min read
Basic Shell Commands in Linux

In the world of Unix/Linux, shell commands are essential for interacting with the operating system and managing files. This blog post will introduce you to fundamental commands, their usage, and how to navigate the file system effectively.

1. Navigating the File System

ls - List Directory Contents

The ls command lists the files and directories in the current working directory.

ls          # List files and directories
ls -l       # List in long format (detailed view)
ls -a       # List all files, including hidden files

cd - Change Directory

The cd command allows you to change your current directory.

cd /path/to/directory  # Navigate to a specific directory
cd ..                  # Go up one directory
cd ~                   # Go to the home directory

2. Handling Files and Directories

cp - Copy Files and Directories

The cp command copies files or directories.

cp source.txt destination.txt  # Copy a file
cp -r source_directory/ target_directory/  # Copy a directory recursively

mv - Move or Rename Files and Directories

The mv command moves files or directories and can also rename them.

mv oldname.txt newname.txt  # Rename a file
mv file.txt /path/to/destination/  # Move a file

rm - Remove Files and Directories

The rm command deletes files or directories.

rm file.txt                  # Remove a file
rm -r directory/             # Remove a directory and its contents recursively

mkdir - Create a New Directory

The mkdir command creates a new directory.

mkdir new_directory          # Create a new directory

3. Working with File Contents

echo - Display a Line of Text

The echo command prints text or variables to the terminal.

echo "Hello, World!"        # Print a message

cat - Concatenate and Display File Contents

The cat command displays the contents of a file.

cat file.txt                # Display the content of a file

touch - Create an Empty File

The touch command creates an empty file or updates the timestamp of an existing file.

touch newfile.txt           # Create a new empty file

4. Searching and Processing Text

grep - Search for Patterns in Files

The grep command searches for specific patterns within files.

grep "search_term" file.txt  # Search for a term in a file

awk - Pattern Scanning and Processing Language

The awk command is used for pattern scanning and processing.

awk '{print $1}' file.txt    # Print the first column of a file

sed - Stream Editor for Filtering and Transforming Text

The sed command edits text in a stream.

sed 's/old/new/g' file.txt    # Replace 'old' with 'new' in a file

find - Search for Files and Directories

The find command searches for files and directories within a specified path.

find /path -name "filename"   # Find files with a specific name

5. Managing Permissions

chmod - Change File Permissions

The chmod command modifies file permissions.

chmod 755 file.txt            # Set permissions to read, write, and execute for owner, and read and execute for others

Conclusion

Mastering these basic shell commands will greatly enhance your ability to navigate and manage files in a Unix/Linux environment. Whether you’re handling files, searching for specific content, or managing permissions, these commands are fundamental tools in your toolkit.

Practice these commands in your terminal, and soon you'll become proficient in managing your file system efficiently!

More from this blog

Dinesh's Blog

104 posts

Basic Shell Commands in Linux