Frequently Used Linux Commands

Introduction

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

Linux Commands
 

List

1) Show all files and directories.

Syntax

ls

2) List out all files including hidden files.

Syntax

ls -a

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

Syntax

ls -l

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

Syntax

ls -l

Directory

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

Syntax

mkdir -m 777 <directory_name>

Example

mkdir -m 777 sample_folder

2) Delete the directory.

Syntax

rmdir <directory_name>

Example

rmdir sample_folder

3) Rename the directory.

Syntax

mv <directory_name> <new_directory_name>

Example

mv sample_folder test_folder

4) To change the current directory location.

Syntax

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

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

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

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

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

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

mv <old_file_name> <new_file_name>

Example

mv sample.txt test.txt

6) Copy the existing file with the data.

Syntax

cp <source_file_name> <newe_file_name>

Example

cp sample.txt test.txt

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

Syntax

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

chmod <permission> <existing_file_name>

Example

chmod 777 test.txt

9) Show the first 10 lines of text file.

Syntax

head <file_name>

Example

head test.txt

10) Show the last 10 lines of text file.

Syntax

tail <file_name>

Example

tail test.txt

File Compression

1) Compress the files.

Syntax

gzip <file_name>

Example

gzip test.txt

2) Uncompress files compressed by gzip.

Syntax

gunzip <file_name>

Example

gzip test.gz

Find

1) Search the given string in the files.

Syntax

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

clear

History

1) Show history of previous commands.

Syntax

history 

Date

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

Syntax

date

2) Display the calendar of the current month.

Syntax

cal <month> <year>

Example

cal 12 2021 (Print calendar for December 2021)

Network

1) Show the IP address of the system.

Syntax

ip

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

Syntax

ping

Process

1) List the processes.

Syntax

ps

2) Kill the processes.

Syntax

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

man <command_name>

Example

man ls 

About

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

Syntax

who

2) Display your username.

Syntax

whoami

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

Syntax

last username

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


Similar Articles