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

5.1 - Linux Commands

le fichier contient la formation du système d'exploitation linux

Uploaded by

Chega Kitubanza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

5.1 - Linux Commands

le fichier contient la formation du système d'exploitation linux

Uploaded by

Chega Kitubanza
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

The Basic Commands Of Linux.

Mr. Taha Kassem


Remote Sensing Specialist and Information Systems Manager , Nile Forecast Center, Nile Water Sector ,MWRI
1.ls - List directory contents
The ls command in Linux is one of the most frequently used commands for listing
directory contents. It displays files, directories, and their properties. Here are
explanations of common options and examples to help illustrate how ls works.
The ls command is fundamental in Linux, and mastering its options can significantly
improve navigation and file management efficiency.
ls - List directory contents
ls : List files and directories in the current directory.
ls -a : List files , list hidden files.
ls -l : Detailed listing with file permissions, size, etc.
ls -lh : Human-readable file sizes.
ls /path/to/directory : List contents of a specific directory
ls –la :Show a detailed list, including hidden files.
ls –lS :Sorts the output by file size, with the largest files at the top.

ls –lt : List by Modification Time (-t)


ls - List directory contents
ls : List files and directories in the current directory.
$ls –lh : Show Human-Readable Sizes (-h)
Displays file sizes in a human-readable format (e.g., KB, MB, GB) instead of bytes.

$ls –lr :Reverse Order (-r)


Reverses the order of sorting. This is useful when combined with other options, like -t or -S.

$ls –R :Recursive Listing (-R)


Lists all files and directories recursively, including those within subdirectories.

$ls -d */ :List Only Directories (-d)


Displays only directories, excluding files.
Examples:
ls : List files and directories in the current directory.
ls –a : Show all files, including hidden ones.
ls –l : Display files in a detailed list format.
ls –lh : Show human-readable file sizes.
ls –R : List directories recursively.
ls –t : Sort files by modification time.
ls –S : Sort files by size.
ls /home/user : List contents of a specific directory.
ls -d */ : List only directories.
ls *.txt : List only .txt files.
ls –I : Show inode numbers with files.
ls –X : Sort files by extension.
ls –ltr : List files sorted by modification date, oldest first.
ls -1 : List one file per line.
ls –F : Append symbols to show file types.
ls –lS : List files by size in descending order.
ls ~ : List files in the home directory.
ls –p : Add a / at the end of directory names.
ls -R /var : Recursively list all files in /var.
ls --color=auto : Display color-coded output.
Examples:
1-$ ls –l *.jpg
2-$ ls –l *.pdf
3-$ls -l *.{a..z}
4-$ls -l *.b
5-$ls –l d*
Exercises
Example 1: Listing Files with Human-Readable Sizes
Example 2: Displaying Hidden Files in the Home Directory
Example 3: Sorting Files by Time Modified in Reverse Order
Example 4: Listing Files Recursively
Example 5: Checking the Type of Files
Example 6: Grouping Files by Extension (-X)
Exercise 7: Check File Sizes in Human-Readable Format
Exercise 8: Sorting Files
Homework
1. Use ls to list files in your current directory.
2. List hidden files in the home directory.
3. Show detailed information for each file in /usr/bin.
4. List files in /var/log sorted by modification date.
5. Display .txt files only.
6. Show only directories in the current directory.
7. List files sorted by size in descending order.
8. Use ls -ltr to list files with the oldest first.
9. Display files with inode numbers.
10. List files and display color-coded output.
11. List all files in /etc recursively.
12. Show one file per line in /bin.
13. List files with file sizes in a human-readable format.
14. Display files with symbols to indicate file types.
15. Sort files in your home directory by extension.
16. Show only the directories in /home.
17. Display files in the current directory, sorted by size.
18. List all .conf files in /etc.
19. Show all files in /usr without sorting.
20. Use ls -lhS in /var/log.
2. cd Change directory
The cd command is used to navigate between directories.
2. cd Change directory
cd /home/user : Go to a specific directory.
cd .. : Move up one directory.
cd - : Return to the previous directory.
cd ~ : Go to the home directory.
cd / : Go to the root directory.
Navigating to a Directory:
cd directory_name
Returning to Home Directory:
cd ~
Going Up One Level:
cd ..
cd ../../ :go up two folders.
1. cd /home : Navigate to /home.
Examples: 2. cd ~ : Go to home directory.
3. cd .. : Move up one level.
4. cd - : Return to the last directory.
5. cd /var/log : Go to system logs.
6. cd ~/Documents : Navigate to Documents.
7. cd / : Go to root directory.
8. cd /usr/local/bin : Navigate to /usr/local/bin.
9. cd /mnt : Go to the mount directory.
10. cd /tmp : Change to /tmp.
11. cd ~/Desktop : Navigate to Desktop.
13. cd /etc/ : Go to configuration directory.
14. cd /proc : Change to /proc.
15. cd ~/Downloads : Go to Downloads folder.
16. cd "$HOME/Documents“ : Go to Documents with $HOME.
17. cd /usr/bin : Change to /usr/bin.
18. cd ~/Desktop/Projects : Go to nested Desktop folder.
20. cd /opt : Navigate to /opt.
Exercises 1.
2.
Change to the home directory.
Go to /var and return to the previous directory.
3. Move to /usr/bin.
4. Use cd to navigate to the Desktop.
5. Go to /etc/systemd.
6. Use cd to change to /tmp.
7. Move up one level with cd ...
8. Go to /home/username.
9. Navigate to /opt.
10. Change to /usr/local and list contents.
11. Use cd - to toggle between two directories.
12. Go to /mnt and back to home.
13. Use cd with spaces in a directory name.
14. Change to /etc/cron.d.
15. Use cd in Downloads.
16. Go to /home and back up to /.
17. Navigate to /var/log.
18. Go to /dev and print current path.
19. Move to /sys/kernel.
20. Use cd to move into a folder within /tmp.
3- mkdir - Make Directory
The mkdir command is used to create new directories.
• 3- mkdir - Make Directory
mkdir new_directory
Creates a directory named new_directory in the current location.
Creating Nested Directories:
mkdir -p parent/child/grandchild
The -p option creates parent directories as needed, making it easier to
create nested structures.
mkdir new_directory : Create a new directory.
mkdir -p dir1/dir2 : Create nested directories.
mkdir -v new_directory : Verbose mode to show actions
Examples:

1. mkdir project : Create a new directory called project.


2. mkdir -p project/src/components: Create nested directories under project.
3. mkdir ~/Documents/Reports : Create a new directory in the Documents folder.
4. mkdir backup : Create a backup directory in the current location.
5. mkdir /tmp/testdir : Create a directory in the /tmp location.
6. mkdir -v new_folder : Print a message for each created directory.
7. mkdir {dir1,dir2,dir3} : Create multiple directories at once.
8. mkdir -p dirA/dirB/dirC : Create nested directories at different levels.
9. mkdir -p /data/logs/{error,access,debug}: Create multiple nested directories.
10. mkdir "$HOME/newdir“ : Create a directory in the home directory using $HOME.
11. mkdir /var/tmp/backup : Create a temporary backup directory.
12. mkdir -v new_project/sub_folder: Create a nested folder with verbose output.
13. mkdir "My Documents“ : Create a directory with a space in the name.
Examples:

$ mkdir Dir_{01..100} :create 100 folders like Dir_01 … Dir_100


$mkdir Dir_{a..z}
$mkdir data_{A..Z}
$mkdir data.{01..100}
Exercises
1. Create a directory named my_files in your home directory.
2. Create nested directories named backup/2024/October.
3. Create three directories named docs, images, and videos at once.
4. Use mkdir -v to create a directory and view confirmation messages.
5. Create directories named project1, project2, and project3 in one command.
6. Use mkdir to create nested directories in /tmp/test/dir1/dir2/dir3.
7. Create a directory with the name "My Work" containing spaces.
8. Make a directory called shared in /home.
9. Make a directory named my_project and enter it.
10. Create a folder named public_html in your Documents.
5-pwd - Print working directory
Displays the current directory path.
The pwd command in Bash (Linux OS) stands for "print working directory“
It displays the full path of the current directory you are in.
$ pwd : Show the full path of the current directory.
Examples:
1. pwd : Show the current directory path.
2. cd /tmp && pwd : Move to /tmp and show the path.
3. cd /home && pwd : Display the path of the home directory.
4. pwd > path.txt : Save the path to a file.
5. mkdir test && cd test && pwd : Print the path after creating a new directory.
8. cd /var/log && pwd : Move to /var/log and print the path.
9. cd ~/Documents && pwd : Display the Documents path.
10. cd / && pwd : Go to root and print the path.
11. cd .. && pwd : Move up and print the new path.
12. cd /usr/bin && pwd : Go to /usr/bin and show the path.
13. pwd | tee current_path.txt : Save and display the path.
14. echo "Current directory: $(pwd)“ : Show with message.
15. cd /etc && pwd : Go to /etc and print the path.
16. cd ~/Desktop && pwd : Print the Desktop path.
17. pwd –L : Print the current directory using logical path.
18. cd /home && pwd : Show the home directory path.
19. cd /mnt && pwd : Print the mount directory path.
20. cd /usr/local && pwd : Show /usr/local directory path.
Exercises
1. Print the current directory path.
2. Move to /tmp and display the path.
3. Navigate to /var and use pwd.
4. Print the home directory path.
5. Create a folder, move to it, and print the path.
6. Use pwd to save the path to a file.
7. Display the path after changing to a new directory.
8. Move up one directory level and use pwd.
9. Print the path of the current directory in /usr/bin.
10. Use pwd -P to see the physical path.
11. Save the current path with tee.
12. Print the path after going to the root.
13. Go to a directory with symbolic links and check the path.
14. Use pwd -L to show the logical path.
15. Print the path in /home/username/my_files.
16. Move to a subfolder within home and print the path.
17. Use pwd in a deeply nested directory.
18. Display the path with a message.
19. Go to /mnt and print its path.
20. Print the path of /usr/local with a message.
6-cp - Copy files or directories
Examples: cp - Copy files or directories

The cp command copies files and directories from one location to another.
Copying a File:
cp file1.txt /path/to/destination
Copying a Directory:
cp -r directory_name /path/to/destination
The -r option allows recursive copying, which is necessary for directories.

cp file1 file2 : Copy file1 to file2.


cp file1 /destination/directory/ : Copy file to another directory.
cp -r dir1 dir2 : Copy directory recursively.
cp -i file1 file2 : Prompt before overwriting.
cp -v file1 file2 : Verbose mode to display actions.
Exercises
1. cp file1.txt file2.txt : Copy file1 to file2.
2. cp -r dir1 dir2 : Copy a directory and its contents.
3. cp /tmp/file ~/ : Copy a file to home directory.
4. cp *.txt /backup/ : Copy all .txt files to /backup.
5. cp file1 file2 ~/Documents : Copy multiple files.
6. cp -v file /tmp : Copy with verbose output.
7. cp ~/file ./ : Copy file to current directory.
8. cp -u file1 file2 : Copy if the destination is older.
9. cp --preserve=mode file1 file2 : Preserve permissions.
10. cp -i file /backup : Prompt before overwriting.
11. cp -R folder1 folder2 : Recursively copy a folder.
12. cp -p file1 /backup : Preserve timestamps.
13. cp -l file1 link1 : Create a hard link.
14. cp -s file1 link1 : Create a symbolic link.
15. cp -a dir1 dir2 : Copy files with attributes.
16. cp -n file1 /backup : Do not overwrite.
17. cp --backup=numbered file1 /backup : Add backups.
18. cp -f file1 file2 : Force copy.
19. cp -H link1 link2 : Copy symlinks.
20. cp --no-preserve=ownership file1 file2 : Ignore ownership.
Exercises
7- mv - Move or rename files or directories
( The mv command moves or renames files and directories)
Examples: mv

Moving a File:
mv file1.txt /path/to/destination
Renaming a File:
mv oldname.txt newname.txt
mv file1 file2 : Rename file1 to file2.
mv file1 /destination/directory/ : Move file to another directory.
mv -i file1 file2 : Prompt before overwriting.
mv dir1 /destination/directory/ : Move a directory.
Exercises

1. Move file1.txt from my_project to a new location.


2. Rename file1.txt to file2.txt
8-rm - Remove files or directories
rm file1: Delete a file.
rm -i file1: Prompt before deleting.
rm -r dir1: Delete a directory recursively.
rm -rf dir1: Force delete a directory and its contents.
rm -v file1: Verbose mode to show actions.
9- touch - Create empty files or update file timestamps
touch newfile : Create a new empty file.
touch -c file1
: Update file timestamp without creating if it doesn’t exist.
touch -a file1 : Change access time only.
touch file1.txt file2.txt file3.txt
touch existing_file.txt :Update Timestamps of an Existing File
touch -t 202411151200 file1.txt : Set a Specific Timestamp
9- touch - Create empty files or update file timestamps
$touch {a..z}.jpg
$touch {a..z}.pdf
10-cat - Concatenate and display file content
cat - Concatenate and Display File Content
The cat command displays the contents of files.
1- Viewing a File:
cat file.txt
2- Concatenating Files:
cat file1.txt file2.txt > combined.txt
Combines file1.txt and file2.txt into a new file combined.txt.
Exercises:
Create two files, add some content to each, and use cat to display their contents.
Concatenate both files into a new file.

1. cat file1: Display file content.


2. cat file1 file2: Display multiple files sequentially.

3. cat -n file1: Number each line of file content.

4. cat file1 > file2: Copy content of file1 to file2.

5. cat file1 >> file2: Append content of file1 to file2


11- echo - Display text
echo "Hello World“ : Print text to the terminal.
echo $PATH : Display the value of an environment variable.
echo "Text" > file.txt : Write text to a file.
echo "Text" >> file.txt : Append text to a file.
echo -n "Hello“ : Print text without a newline.
12- ln - Create Links
The ln command creates both hard and symbolic links.

Symbolic Link (Symlink):


ln -s /path/to/file symlink_name
The -s option creates a symbolic link.
Hard Link:
ln file.txt link_to_file.txt
Exercises:
Create a hard link for a file in the same directory.
Create a symbolic link in another directory.
13- head - Display the first lines of a file
head file1: Display the first 10 lines.
head -n 5 file1: Display the first 5 lines.
head -c 20 file1: Display the first 20 bytes.

14- tail - Display the last lines of a file


tail file1: Display the last 10 lines.
tail -n 5 file1: Display the last 5 lines.
tail -f logfile: Continuously display appended lines (useful for logs).
tail -c 20 file1: Display the last 20 bytes.

14- chmod - Change file permissions

1-Setting Permissions Using Numbers:


chmod 755 file.txt
2-Setting Permissions Using Symbols:
chmod u+x file.txt
Grants execute permissions to the user (u+x).
Exercises:
1. Create a file and view its permissions with ls -l.
2. Use chmod 644 to set its permissions to read/write for the owner and read-only for others.
3. chmod 755 file1: Set read, write, and execute permissions for owner; read and execute for others.
4. chmod +x file1: Make a file executable.
5. chmod u=rwx,g=rx,o=r file1: Set specific permissions for user, group, others.
15-chown - Change file ownership
chown - Change Ownership
The chown command changes file and directory ownership.
Changing Owner:
chown user file.txt
Changing Owner and Group:
chown user:group file.txt

Exercises:
Create a file and change its owner to another user (if you have permissions).
Change the group ownership of a file.
chown user file1: Change ownership to a user.
chown user:group file1: Change user and group ownership.
chown -R user:group dir1: Change ownership recursively for a directory.
df - Report file system disk space usage

• df: Show disk usage for all mounted filesystems.

• df -h: Human-readable format.

• df -T: Show filesystem type.


du - Estimate file and directory space usage
• du: Show disk usage for current directory.

• du -h: Human-readable format.

• du -sh dir1: Show total size of a specific directory.

• du -a: Show size of all files and directories.


free - Displays memory usage
• Options Description • -s, –seconds Continuously displays the output
after ‘s’ seconds delay. Uses the usleep system call
• -k, –kilo Displays memory usage in kilobytes for microsecond resolution delay times.
(default).
• -t, –total Adds an additional line in the
• -m, –mega Displays memory usage in megabytes. output showing column totals.

• -g, –giga Displays memory usage in gigabytes. • –help Displays a help message and exits.

• -V, –version Displays version information and


• –tera Displays memory usage in terabytes.
exits.
• -h, –human Automatically scales all output
columns to the shortest three-digit unit and
displays the units (B, K, M, G, T).

• -c, –count Displays the output ‘c’ number of


times; works with the -s option.

• -l, –lohi Shows detailed low and high memory


statistics.

• -o, –old Disables the display of the buffer-


adjusted line.
history - Show command history
• history: Display the history of commands.

• history | grep "command": Search for specific command in history.

• !100: Execute the command at line 100 in history.


top - Display Linux tasks

• top: Show running processes.

• top -u username: Show processes for a specific user.

• top -p PID: Track a specific process.


tar - Archive files

• tar -cvf archive.tar dir1: Create an archive of a directory.

• tar -xvf archive.tar: Extract files from an archive.

• tar -czvf archive.tar.gz dir1: Compress directory with gzip.

• tar -xzvf archive.tar.gz: Extract gzip compressed archive.


zip - Compress files

• zip archive.zip file1: Compress a file.

• zip -r archive.zip dir1: Compress a directory recursively.

• unzip archive.zip: Extract a zip archive.


ps - Report a snapshot of current processes

• ps: Show processes for the current shell.

• ps aux: Show all processes with details.

• ps -u username: Show processes for a specific user.

• ps -ef | grep process_name: Search for a process by name.


ps - Report a snapshot of current processes

• ps: Show processes for the current shell.

• ps aux: Show all processes with details.

• ps -u username: Show processes for a specific user.

• ps -ef | grep process_name: Search for a process by name.


uname - display system information
The uname command in Linux is used to display system information such as the operating
system, kernel version, and hardware details. It is often used by administrators to gather basic
system information.
grep - Search text in files
• grep "pattern" file1: Search for a pattern in a file.

• grep -i "pattern" file1: Case-insensitive search.

• grep -r "pattern" /path/to/dir: Recursive search in a directory.

• grep -n "pattern" file1: Show line numbers for matches.

• grep -v "pattern" file1: Display lines that do not match.

You might also like