Introduction

In this article, you will learn about frequently used Linux commands with examples.

Linux Commands

List

1) Show all files and directories.

Syntax

Bash
ls

2) List out all files including hidden files.

Syntax

Bash
ls -a

3) List the files in 'long format', which contains lots of useful information (size, last modified).

Syntax

Bash
ls -l

4) List out the files with details (timestamp and access details).

Syntax

Bash
ls -l

Directory

1) Creating directory with all permissions (Read, write, and execute).

Syntax

Bash
mkdir -m 777 <directory_name>

Example

mkdir -m 777 sample_folder

2) Delete the directory.

Syntax

Bash
rmdir <directory_name>

Example

rmdir sample_folder

3) Rename the directory.

Syntax

Bash
mv <directory_name> <new_directory_name>

Example

mv sample_folder test_folder

4) To change the current directory location.

Syntax

Bash
cd <directory_name>

Example

cd sample_folder (Switch to a subdirectory (of the current directory) named sample_folder)
cd .. (Move to the parent directory of the current directory)
cd /home/sample_folder (Switch to a directory named /home/sample_folder)

5) Find all of the files in the directory and all of its subdirectories.

Syntax

Bash
find <directory_name> -name <file_name> -print

Example

find . -name myfile.txt -print (Find in the current directory and its subdirectories)
find sample_folder -name myfile.txt -print (Find in the "sample_folder" directory and its subdirectories)

Files

1) Creating the blank files.

Syntax

Bash
touch <file_name> 
touch <file_name_1> <file_name_2> #For creating multiple blank files

Example

touch sample.txt or touch sample.txt text.txt

2) Create a new file using cat command.

Syntax

Bash
cat <file_name>

Example

cat new_test.txt

Note: Awaits input from the user, type desired text, and press CTRL+D (hold down Ctrl key and type ‘d‘) to exit.

3) Delete the file.

Syntax

Bash
rm <file_name>

Example

rm sample.txt (Deletes the file named "sample.txt")
rm sample.txt test.txt (Deletes two files – "sample.txt" and "test.txt")
rm new* (Deletes all files that begin with the prefix "new")

4) Move the file from one directory to another directory.

Syntax

Bash
mv <directory_name/file_name> <new_directory_name/file_name>

Example

mv sample_folder/sample.txt new_folder/sample.txt

5) Rename the file.

Syntax

Bash
mv <old_file_name> <new_file_name>

Example

mv sample.txt test.txt

6) Copy the existing file with the data.

Syntax

Bash
cp <source_file_name> <newe_file_name>

Example

cp sample.txt test.txt

7) Display the contents of the file in the terminal.

Syntax

Bash
cat <existing_file_name>

Example

cat test.txt (Display the contents of "test.txt" file)
cat sample.txt test.txt (Display the contents of two files "sample.txt" and "test.txt" on your terminal as one continuous display)

8) Change the permission of the file.

Syntax

Bash
chmod <permission> <existing_file_name>

Example

chmod 777 test.txt

9) Show the first 10 lines of text file.

Syntax

Bash
head <file_name>

Example

head test.txt

10) Show the last 10 lines of text file.

Syntax

Bash
tail <file_name>

Example

tail test.txt

File Compression

1) Compress the files.

Syntax

Bash
gzip <file_name>

Example

gzip test.txt

2) Uncompress files compressed by gzip.

Syntax

Bash
gunzip <file_name>

Example

gzip test.gz

Find

1) Search the given string in the files.

Syntax

Bash
grep <search_string> <file_name>

Example

grep "test" sample.txt

Clean

1) Clears all the clutter on the terminal and gives the clean window.

Syntax

Bash
clear

History

1) Show history of previous commands.

Syntax

Bash
history

Date

1) Display the current day, date, time, and year.

Syntax

Bash
date

2) Display the calendar of the current month.

Syntax

Bash
cal <month> <year>

Example

cal 12 2021 (Print calendar for December 2021)

Network

1) Show the IP address of the system.

Syntax

Bash
ip

2) Check if remote host is reachable via ICMP ping.

Syntax

Bash
ping

Process

1) List the processes.

Syntax

Bash
ps

2) Kill the processes.

Syntax

Bash
kill <process_id>

Example

kill 1234

Help

1) Display the how to use a particular command or want to find out all its options.

Syntax

Bash
man <command_name>

Example

man ls

About

1) To know about who is logged on, and where they're coming from.

Syntax

Bash
who

2) Display your username.

Syntax

Bash
whoami

3) To know about when the user last logged on and off and from where.

Syntax

Bash
last username

Hope you know about the list of Unix commands which are frequently used. Thank you.