0% found this document useful (0 votes)
2 views

Basic Commands with explanation

The document provides a comprehensive guide on basic Linux commands, including file listing, changing directories, creating files and directories, and copying, moving, and deleting files. Each command is accompanied by its purpose, examples, and expected output. The guide serves as a practical reference for users to navigate and manage files in a Linux environment.

Uploaded by

akshithreddy849
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Basic Commands with explanation

The document provides a comprehensive guide on basic Linux commands, including file listing, changing directories, creating files and directories, and copying, moving, and deleting files. Each command is accompanied by its purpose, examples, and expected output. The guide serves as a practical reference for users to navigate and manage files in a Linux environment.

Uploaded by

akshithreddy849
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Basic Commands

1.ls

 Purpose: Lists the files and directories in the current directory.


 Example:

ls

Output:

file1 file2 dir1 dir2

2. ls -a

 Purpose: Lists all files, including hidden files (those starting with a dot .).
 Example:

ls -a

Output:

. .. .hidden_file file1 file2 dir1 dir2

3. ls -l

 Purpose: Displays detailed information about files and directories (permissions,


owner, size, modification date).
 Example:

ls -l

Output:

-rw-r--r-- 1 user user 1024 Jan 24 file1


drwxr-xr-x 2 user user 4096 Jan 24 dir1

4. ls -l collection/

 Purpose: Displays detailed information about the contents of the collection/


directory.
 Example:

ls -l collection/

Output:
-rw-r--r-- 1 user user 512 Jan 24 doc1.txt
drwxr-xr-x 2 user user 4096 Jan 24 images

5. ls -ld collection/

 Purpose: Displays detailed information about the collection/ directory itself, not
its contents.
 Example:

ls -ld collection/

Output:

drwxr-xr-x 5 user user 4096 Jan 24 collection/

6. ls -i

 Purpose: Displays the inode number of each file and directory.


 Example:

ls -i

Output:

12345 file1 12346 file2 12347 dir1

7. ls -lt /home/student/

 Purpose: Lists the contents of /home/student/ sorted by modification time (newest


first).
 Example:

bash
ls -lt /home/student/

Output:

-rw-r--r-- 1 user user 2048 Jan 24 report.pdf


drwxr-xr-x 3 user user 4096 Jan 20 projects

8. ls -ltr /home/student/

 Purpose: Lists the contents of /home/student/ sorted by modification time (oldest


first).
 Example:
ls -ltr /home/student/

Output:

drwxr-xr-x 3 user user 4096 Jan 20 projects


-rw-r--r-- 1 user user 2048 Jan 24 report.pdf

9. ls -sh /home/student/

 Purpose: Displays the sizes of files in a human-readable format.


 Example:

ls -sh /home/student/

Output:

4.0K report.pdf 8.0K projects

10. ls -s /boot

 Purpose: Displays the size of each file in blocks (default block size).
 Example:

ls -s /boot

Output:

32 config-5.11.0 64 initrd.img

11. ls -R /boot

 Purpose: Recursively lists all files and subdirectories in /boot.


 Example:

ls -R /boot

Output:

/boot:
config-5.11.0 initrd.img
/boot/grub:
menu.lst

12. ls -R /home/

 Purpose: Recursively lists all files and subdirectories in /home.


 Example:
ls -R /home/

Output:

/home/user:
documents downloads

/home/user/documents:
file1.txt file2.pdf

13. tree /home

 Purpose: Displays the directory structure in a tree-like format. (You may need to
install tree first.)
 Example:

tree /home

Output:

/home
├── documents
├── downloads
└── pictures

14. history

 Purpose: Displays the history of all previously executed commands.


 Example:

history

Output:

1 ls
2 cd /home
3 pwd

2. Change Directory (cd)

 Change to /etc directory:


Example:

cd /etc
pwd
Output:

/etc

 Go back to the parent directory using cd ..:


Example:

cd ..
pwd

Output:

 Return to the home directory with cd ~:


Example:

cd ~
pwd

Output:

/root

 Using relative path to navigate to a subdirectory:


Example:

cd collection
pwd

Output:

/root/collection

3. Paths

 Absolute Path:
Example:

cd /root/collection
pwd

Output:

/root/collection

 Relative Path:
Example:

cd collection
pwd
Output:

/root/collection

4. File Creation (touch)

 Create a single file:


Example:

touch app
ls

Output:

app

 Create multiple files at once:


Example:

touch data apple grapes orange


ls

Output:

app apple data grapes orange

 Using filename extensions:


Example:

touch app{1..5}.txt
ls

Output:

app1.txt app2.txt app3.txt app4.txt app5.txt

5. Creating a Directory (mkdir)

 Create a single directory:


Example:

mkdir project
ls

Output:

project
 Create multiple directories at once:
Example:

mkdir dir1 dir2 dir3


ls

Output:

dir1 dir2 dir3

 Create nested directories:


Example:

mkdir -p office/departments/{Sales,Marketing,Accounts}
ls office/departments

Output:

Accounts Marketing Sales

6. Copy, Move, and Delete

6.1 Copy Commands

 Copy a single file:


Example:

cp app1.txt dir1/
ls dir1

Output:

app1.txt

 Copy multiple files:


Example:

cp app2.txt app3.txt dir2/


ls dir2

Output:

app2.txt app3.txt

 Copy a directory (recursively):


Example:

cp -r dir1 dir3/
ls dir3
Output:

dir1

6.2 Move Commands

 Move a file to another directory:


Example:

mv app4.txt dir2/
ls dir2

Output:

app2.txt app3.txt app4.txt

 Rename a file:
Example:

mv app5.txt renamed_app5.txt
ls

Output:

renamed_app5.txt

6.3 Delete Commands

 Delete a file:
Example:

rm app1.txt
ls

Output:

app2.txt app3.txt

 Delete multiple files:


Example:

rm app2.txt app3.txt
ls

Output:

dir1 dir2
 Delete a directory and its contents:
Example:

rm -r dir1
ls

Output:

dir2

You might also like