Essential Linux Commands for Beginners

Essential Linux Commands for Beginners

If you're just starting out with Linux, navigating through the command line interface can be a bit daunting. But fear not! With the right set of commands at your disposal, you can swiftly perform various operations, from managing files and directories to finding specific data. Here's a beginner-friendly guide to some essential Linux commands:

Finding Files and Directories

whereis

The whereis command helps you find the path of an executable file.

whereis <executable-file-name>

List Operations

ls

ls is perhaps one of the most commonly used commands in Linux. It lists the contents of a directory.

ls
  • -a option: Lists hidden files.

  • -l option: Displays file permissions.

  • -R option: Shows subdirectories recursively.

Changing Directory Operations

cd

cd is used to change directories.

cd <folder-name>
cd ..  # Go one directory back.
cd     # Go to home directory.
cd ../<foldername>  # Open a previous directory folder.
cd <path>  # Open a directory with the specified path.

File and Directory Operations

mkdir

mkdir creates a new directory.

mkdir <new-dir-name>
mkdir -p test/test1/test2  # Create a directory along with parent directories.

touch

touch creates a new file.

touch <new-file-name>

pwd

pwd displays the present working directory.

pwd

cat

cat is used for displaying or creating files.

cat <filename>  # Display file content.
cat > <new-file-name>  # Create a new file.
cat >> <filename>  # Append to a file.
cat <filename> <filename2>  # Display contents of multiple files.
cat <filename> <filename2> > <newfile-name>  # Merge contents of multiple files into one.
cat <file-name> | tr > <new-file-name>  # Translate the file.

dd

dd creates an empty file with zeros.

dd if=/dev/zero of=bos_dosya bs=4G count=1

cut

cut is used to cut sections from each line of files.

cut -c 1-2 <filename>  # Cut file column-wise.

echo

echo is used to print text to the terminal.

echo "Hello" >> <file-name>

man

man displays the manual pages of commands.

man <command-name>

File and Directory Manipulation

cp

cp copies files or directories.

cp <file-name> <new-file-name>

mv

mv moves or renames files or directories.

mv <file-name> <dir-path>  # Move a file to another directory.
mv <file-name> <new-file-name>  # Rename a file.
mv -R <dir-name> <dir-path>  # Move a directory.

rm

rm removes files or directories.

rm <file-name>  # Remove a file permanently.
rm -R <dir-name>  # Delete a directory recursively.

Viewing File Contents

head

head displays the first few lines of a file.

head <file-name>

tail

tail displays the last few lines of a file.

tail <file-name>
tail -n 2 <file-name>  # Display the last 2 lines.

File Comparison and Searching

diff

diff shows the differences between two files.

diff <file-1> <file-2>

locate

locate finds files by name.

locate <file-name>

find

find searches for files and directories.

find <file/folder-name>  # Find a file or folder.
find <dir-name>  # Find files inside a directory.
find . -type d  # Show only directories.
find . -type f -name "*.txt"  # Show only files with a specific name.
find . -type f -iname "*.txt"  # Show files with a specific name (case-insensitive).
find . -type f -mmin -20  # Show files modified less than 20 minutes ago.
find . -type f -mmin +20  # Show files modified more than 20 minutes ago.
find . -maxdepth 2  # Limit search to one folder deep.
find . -size +1k  # Show files or directories larger than 1KB.

System Commands

ps

The ps command displays information about the processes running on your system.

ps aux

systemctl

systemctl is a powerful command-line utility for controlling system services. You can start, stop, enable, or disable services using this command.

systemctl start <service>
systemctl stop <service>
systemctl enable <service>
systemctl disable <service>

df

df checks the capacity and storage details of filesystems.

df -h  # Display in human-readable format.

du

du displays disk usage statistics for directories.

du -h  # Display in human-readable format.

sudo

sudo is used to execute commands with administrative privileges.

sudo chown root text.txt  # Change owner of a file to root.

!

!<command-name> reruns the previous command with the specified name.

!<command-name>

git

Git commands allow version control operations.

git add . && git commit -m "message"  # Run multiple commands at once.

sort

sort arranges lines of text files.

sort <file-name>

job

job displays the jobs running in the background.

jobs

wget

wget downloads files from the internet.

wget <url>

top

top provides real-time information about processes.

top

kill

kill terminates processes by process ID (PID).

kill <process-id>

uname

uname displays system information.

uname -o  # Operating system.
uname -m  # Machine hardware.
uname -r  # Kernel release.

zip

zip compresses files into a zip archive.

zip <file-1> <file-2>

unzip

unzip extracts files from a zip archive.

unzip <file-name>

useradd and passwd

useradd adds a new user, and passwd sets a password for the user.

useradd <name>
passwd <name>

lscpu

lscpu provides information about the CPU.

lscpu

free

free displays memory usage statistics.

free

vmstat

vmstat provides virtual memory statistics.

vmstat

lsof

lsof lists all open files.

lsof

xdg-open

xdg-open opens files or directories in the default graphical environment.

xdg-open <file-path>
xdg-open .  # Open the current directory.

vi ~/.bashrc

Edit your .bashrc file to set aliases and customize your shell environment.

vi ~/.bashrc

base64

base64 encodes and decodes data in Base64 format.

echo -n 'username' | base64  # Encode username to Base64.
echo -n 'encoded' | base64 -d  # Decode Base64 to plaintext.

Networking

nslookup

nslookup retrieves domain name system (DNS) information.

nslookup google.com

netstat

netstat displays network status and statistics.

netstat

hostname

hostname shows the hostname of the system.

hostname

whoami

whoami displays the current user.

whoami

ping

ping checks network connectivity using Internet Control Message Protocol (ICMP).

ping google.com

Permissions Management

chmod

The chmod command is used to change permissions on files and directories.

chmod u=rwx,g=rxw,o=rwx <file-name>  # Assigns Read, Write, and Execute permissions.
chmod 777 <file-name>  # Numeric representation: 4 for Read, 2 for Write, and 1 for Execute.

find

The find command searches for files and directories based on various criteria, including permissions.

find . -perm 777  # Shows files with all permissions (rwx).

Advanced Command Usage

grep

grep is a powerful tool for searching text patterns within files.

grep <keyword> <file-name>  # Searches if the keyword is present in the file.
grep -w <keyword> <file-name>  # Searches for the complete word.
grep -i <keyword> <file-name>  # Searches case-insensitively.
grep -n <keyword> <file-name>  # Shows line numbers where the keyword is present.
grep -B <keyword> <file-name>  # Shows lines before the keyword.
grep -win <keyword> ./*.txt  # Searches for the keyword in text files in the current directory.

history

The history command lists previously executed commands.

history  # Displays command history.
!<number-from-history>  # Re-executes a command from history.

Advanced Command Operators

Piping and Logical Operators

ping google.com & ping facebook.com  # Run both commands simultaneously.
echo "google.com" && echo "facebook.com"  # Second command runs only if the first is successful.
echo "google.com" && {echo "facebook.com"; echo "pradumnasaraf.co"}  # Grouping commands.
echo "google.com" || echo "pingfacebook.com"  # Second command runs only if the first fails.
rm -r !(file.txt)  # Delete all files except file.txt.

Environment Variables

printenv

printenv displays all environment variables.

printenv  # Prints all environment variables.

These are few commands that would help you to start off your Linux journey.