Skip to main content

Command Palette

Search for a command to run...

Linux Intermediate Part1

Updated
•10 min read•View as Markdown
Linux Intermediate Part1

madir -m 700: mkdir -m 700 ~/project/digital_garden/private

  • mkdir: Create directory

  • -m 700 sets permission

  • 7 Read Write and Execute for the owner

  • 0 No permission for group

  • 0 No permission for other

  • It mean only you as owner can access this directory

Check the permission with below command and output

labex:digital_garden/ $ ls -ld ~/project/digital_garden/private
drwx------ 2 labex labex 6 Sep 24 04:41 /home/labex/project/digital_garden/private

Here, drwx------ means:

  • d: It's a directory

  • rwx: You (the owner) have read, write, and execute permissions

  • ------: Neither your group nor others have any permissions

mkdir -v : vervose mode. It is helpful when creating multiple directories. It gives feedback in ach creation.

labex:digital_garden/ $ mkdir -v ~/project/digital_garden/resources/books ~/project/digital_garden/resources/articles ~/project/digital_garden/resources/videos
mkdir: created directory '/home/labex/project/digital_garden/resources/books'
mkdir: created directory '/home/labex/project/digital_garden/resources/articles'
mkdir: created directory '/home/labex/project/digital_garden/resources/videos'
labex:digital_garden/ $ mkdir -pvm 750 ~/project/digital_garden/projects/research_paper/drafts ~/project/digital_garden/projects/research_paper/references
mkdir: created directory '/home/labex/project/digital_garden/projects/research_paper'
mkdir: created directory '/home/labex/project/digital_garden/projects/research_paper/drafts'
mkdir: created directory '/home/labex/project/digital_garden/projects/research_paper/references'

Let's break this down:

  • -p: Create parent directories as needed

  • -v: Verbose mode, print a message for each created directory

  • -m 750: Set permissions (owner: full access, group: read and execute, others: no access)

This command creates two directories (drafts and references) inside research_paper, which itself is created inside projects if it doesn't already exist.

tree: This command show the directory structure in a tree format.

labex:digital_garden/ $ tree ~/project/digital_garden 
/home/labex/project/digital_garden
├── notes
├── private
├── projects
│   ├── research_paper
│   │   ├── drafts
│   │   └── references
│   └── web_app
│       └── src
│           └── components
└── resources
    ├── articles
    ├── books
    └── videos

13 directories, 0 files

tree -p: This is to see with permission details.

labex:digital_garden/ $ tree -p ~/project/digital_garden
[drwxrwxr-x]  /home/labex/project/digital_garden
├── [drwxrwxr-x]  notes
├── [drwx------]  private
├── [drwxrwxr-x]  projects
│   ├── [drwxrwxr-x]  research_paper
│   │   ├── [drwxr-x---]  drafts
│   │   └── [drwxr-x---]  references
│   └── [drwxrwxr-x]  web_app
│       └── [drwxrwxr-x]  src
│           └── [drwxrwxr-x]  components
└── [drwxrwxr-x]  resources
    ├── [drwxrwxr-x]  articles
    ├── [drwxrwxr-x]  books
    └── [drwxrwxr-x]  videos

13 directories, 0 files

cp: This is to copy file from or duplicating file. Below is the example of coping file in the same directory.

labex:project/ $ cp ~/project/important_report.txt ~/project/important_report_backup.txt
labex:project/ $ ls ~/project
documents                    music              pdf_files    report2.txt
favorite_song.mp3            new_test_file.txt  report1.pdf  test_file.txt
important_report_backup.txt  notes.txt          report1.txt  text_files
important_report.txt         old_file.txt       report2.pdf  website
labex:project/ $

Below is the example of coping file into different directory. Coping .mp3 file from project to music directory.

labex:project/ $ cp ~/project/favorite_song.mp3 ~/project/music/
labex:project/ $ ls ~/project/music
favorite_song.mp3
labex:project/ $

#copying multiple file at once
labex:project/ $ cp ~/project/report1.txt ~/project/report2.txt ~/project/notes.txt ~/project/documents/
labex:project/ $ ls ~/project/documents
notes.txt  report1.txt  report2.txt

cp -i : interactive coping. Overwrite the file if already exist. Once given the prompt, type y or n. In the below example, a file named new_test_file.txt is copied to test_file.txt(where it already exists).

labex:project/ $ cat ~/project/test_file.txt
Original content
labex:project/ $ cp -i ~/project/new_test_file.txt ~/project/test_file.txt
cp: overwrite '/home/labex/project/test_file.txt'? y
labex:project/ $ cat ~/project/test_file.txt ## you can see the new content here
New content

cp -r : recursive coping. This is for copying the directories and its conent recursively. it is usually helpful when taking backup of entire directory structure. In the below example we are taking a backup of entire website directory.

labex:project/ $ cp -r ~/project/website ~/project/website_backup
labex:project/ $ ls -R ~/project/website_backup
/home/labex/project/website_backup:
css  images  index.html  js

/home/labex/project/website_backup/css:
style.css

/home/labex/project/website_backup/images:
logo.png

/home/labex/project/website_backup/js:
script.js

cp - p: This is for preserving the original file attributes such as timestamp and permission. In the below example we are copying file from old_file.txt to new_file.txt

labex:project/ $ ls -l ~/project/old_file.txt
-rw-rw-r-- 1 labex labex 17 Sep 25 17:08 /home/labex/project/old_file.txt
labex:project/ $ cp -p ~/project/old_file.txt ~/project/preserved_file.txt
labex:project/ $ ls -l ~/project/old_file.txt ~/project/preserved_file.txt
-rw-rw-r-- 1 labex labex 17 Sep 25 17:08 /home/labex/project/old_file.txt
-rw-rw-r-- 1 labex labex 17 Sep 25 17:08 /home/labex/project/preserved_file.txt

*: wildcard copying. In the below example, all files with .txt and .pdf copied using * to text_files and pdf_files respectively.

labex:project/ $ cp ~/project/*.txt ~/project/text_files 
labex:project/ $ cp ~/project/*.pdf ~/project/pdf_files/
labex:project/ $ ls ~/project/text_files
important_report_backup.txt  notes.txt           report1.txt
important_report.txt         old_file.txt        report2.txt
new_test_file.txt            preserved_file.txt  test_file.txt
labex:project/ $ ls ~/project/pdf_files
report1.pdf  report2.pdf

Additional cp options.

  • -u: Copy only when the source file is newer than the destination file or when the destination file is missing

  • -v: Verbose mode, explaining what is being done

  • -n: No clobber; do not overwrite an existing file

  • -l: Create hard links instead of copying files

  • -s: Create symbolic links instead of copying files

mv: This is for moving or renaming files.

labex:project/ $ mkdir css
labex:project/ $ mv styles.css css/
labex:project/ $ cd css 
labex:css/ $ ls
styles.css

#renaming index.html to home.html
labex:project/ $ mv index.html home.html
labex:project/ $ ls
css  home.html  images  script.js  utils.js

##moving all .js files to scripts directory
labex:project/ $ mkdir scripts
labex:project/ $ mv *.js scripts/
labex:project/ $ cd scripts 
labex:scripts/ $ ls
script.js  utils.js

mv -i: moving file in an interactive way. The below command is asking if if we want to overwrite home.html from test.txt

labex:project/ $ echo "Test content" > test.txt
labex:project/ $ ls
css  home.html  images  scripts  test.txt
labex:project/ $ echo "Home page" > home.html
labex:project/ $ mv -i test.txt home.html
mv: overwrite 'home.html'? y

rm: This is to remove the file.

labex:project/ $ ls old_report.txt
old_report.txt
labex:project/ $ rm old_report.txt 
labex:project/ $ rm old_report.txt
rm: cannot remove 'old_report.txt': No such file or directory

##removing multiple files
labex:project/ $ ls *.tmp
file1.tmp  file2.tmp  file3.tmp
labex:project/ $ rm file1.tmp file2.tmp file3.tmp
labex:project/ $ ls *.tmp                        
ls: cannot access '*.tmp': No such file or directory

rm -r: This is to remove a directory.

labex:project/ $ ls old_projects 
project1.txt  project2.txt
labex:project/ $ rm old_projects 
rm: cannot remove 'old_projects': Is a directory
labex:project/ $ rm -r old_projects 
labex:project/ $ ls old_projects
ls: cannot access 'old_projects': No such file or directory

rm -i: This is to remove file or directories in a interactive way.

labex:project/ $ ls important_file.txt
important_file.txt
labex:project/ $ rm -i important_file.txt
rm: remove regular file 'important_file.txt'? y

cat: By this command, we can view the content of the file directly in the terminal.

labex:project/ $ cat daily_report.txt
Daily Report: August 8, 2024

#cat command is also used to combine multiple files.
labex:project/ $ cat marketing.txt 
Marketing campaign results
labex:project/ $ cat sales.txt marketing.txt 
Sales figures for Q2
Marketing campaign results

In the below example, the content of the 2 files will be combined to another file using > symbol

labex:project/ $ cat sales.txt marketing.txt > combined_report.txt
labex:project/ $ cat combined_report.txt 
Sales figures for Q2
Marketing campaign results
  • cat -n: This is to show the content with line number.

  • cat -E: This is to make the end of line visible.

labex:project/ $ cat -n daily_report.txt
     1    Daily Report: August 8, 2024
labex:project/ $ cat daily_report.txt 
Daily Report: August 8, 2024
labex:project/ $ cat -E daily_report.txt
Daily Report: August 8, 2024$
  • more: This is to view and navigate the large text files.

  • To navigate through the file:

    • Press the Space bar to move to the next page.

    • Press Enter to move down one line.

    • Press b to go back one page.

    • Press q to quit and return to the command prompt.

labex:project/ $ ls
weather_data.txt
labex:project/ $ more weather_data.txt 
Date,Temperature,Humidity,Precipitation
2023-01-01,23,69,0.654499
2023-01-02,23,69,0.654499
2023-01-03,23,69,0.654499

##The below command will show the content from line no 100
labex:project/ $ more +100 weather_data.txt
2023-04-09,7,43,0.215095

##The below command will show only 10 lines at a time.
labex:project/ $ more -10 weather_data.txt
Date,Temperature,Humidity,Precipitation
2023-01-01,23,69,0.654499
2023-01-02,23,69,0.654499
2023-01-03,23,69,0.654499
2023-01-04,23,69,0.654499
2023-01-05,23,69,0.654499
2023-01-06,23,69,0.654499
2023-01-07,23,69,0.654499
2023-01-08,23,69,0.654499
2023-01-09,23,69,0.654499

Find the specific information for the file. In the below example we are getting information - 2023-07-15 from the file.

labex:project/ $ more +/"2023-07-15" weather_data.txt

...skipping
2023-07-13,7,43,0.215095
2023-07-14,7,43,0.215095
2023-07-15,7,43,0.215095
2023-07-16,7,43,0.215095
  • Additional more command options not covered in this lab include:

    • -d: Displays helpful prompts

    • -f: Counts logical lines instead of screen lines

    • -p: Clears the screen before displaying the page

    • -c: Repaints the screen instead of scrolling

    • -s: Squeezes multiple blank lines into one

    • -u: Suppresses underlining

less: This is used for navigating and analyzing large text file(such as log file) page by page. q is for quite the file viewing.

  • Use the following keys to navigate:

    • Press Space or Page Down to move forward one page

    • Press b or Page Up to move backward one page

    • Use the Up and Down arrow keys to move line by line

    • Press G (Shift + g) to go to the end of the file

    • Press g to go to the beginning of the file

labex:project/ $ ls
server_log.txt
labex:project/ $ less server_log.txt 

## To see the line number displayed
labex:project/ $ less -N server_log.txt

## searching for a specific text
labex:project/ $ less +/ERROR:.*Database server_log.txt

head: This is to preview the beginning of the file. By default the head command will show 1st 10 line of each file. -n is used for less number of lines. See the below example.

labex:project/ $ head access.log 
labex:project/ $ head -n 5 access.log 
192.168.1.127 - - [01/Jan/2024:00:00:15 +0000] "DELETE /admin/dashboard HTTP/1.1" 301 3989
192.168.1.184 - - [01/Jan/2024:00:00:46 +0000] "DELETE /style.css HTTP/1.1" 200 2144
192.168.1.181 - - [01/Jan/2024:00:00:09 +0000] "DELETE /comment HTTP/1.1" 500 9335
192.168.1.137 - - [01/Jan/2024:00:00:10 +0000] "POST /login HTTP/1.1" 403 5200
192.168.1.111 - - [01/Jan/2024:00:00:59 +0000] "DELETE /about HTTP/1.1" 302 6020

##if you want to see multiple files
labex:project/ $ head access.log error.log
##To seacrh with specific pattern
labex:project/ $ grep "/admin" access.log | head -n 3

tail: This command is very useful to see end of the file. It’s basic use case is to see recently entered data or latest changes which is usually towards the end. By default tail print 10 lines.

labex:project/ $ tail system.log 
2025-09-28 15:18:40 [INFO] Failed to connect to database server
2025-09-28 15:17:40 [WARNING] Unusual network activity detected

##if you want to print 5 lines in tail
labex:project/ $ tail -n 5 system.log 
2025-09-28 15:13:40 [INFO] Failed to connect to database server
2025-09-28 15:12:40 [INFO] Backup process started
2025-09-28 15:11:40 [INFO] Network interface eth0 is up
2025-09-28 15:10:40 [WARNING] Low memory warning: Available memory below 15%
2025-09-28 15:09:40 [INFO] Backup process started

##if you want to see the end of the file from a specific line, let's say 50. 
##The below command will show it from 50th line
labex:project/ $ tail -n +50 system.log 
2025-09-28 15:59:40 [INFO] Network interface eth0 went down
2025-09-28 15:58:40 [INFO] Scheduled system update initiated

tail -f: f stands for follow. This is useful for watching the log file in real time. This is for watching the file while it is written to. To exit press ctrl + c

labex:project/ $ tail -f system.log 
2025-09-28 15:18:40 [INFO] Failed to connect to database server
2025-09-28 15:17:40 [WARNING] Unusual network activity detected

##Ex - combining -n and -f together
labex:project/ $ tail -n 3 -f /home/labex/project/system.log
2025-09-28 15:10:40 [WARNING] Low memory warning: Available memory below 15%
2025-09-28 15:09:40 [INFO] Backup process started
Sun Sep 28 05:07:17 PM CST 2025 [INFO] New log entry for testing

##enter the following in another command window
labex:project/ $ echo "$(date) [WARNING] Unusual network activity detected" >> /home/labex/project/system.log

##then see the below output where the above is being added to the tail output
labex:project/ $ tail -n 3 -f /home/labex/project/system.log
2025-09-28 15:10:40 [WARNING] Low memory warning: Available memory below 15%
2025-09-28 15:09:40 [INFO] Backup process started
Sun Sep 28 05:07:17 PM CST 2025 [INFO] New log entry for testing
Sun Sep 28 05:10:39 PM CST 2025 [WARNING] Unusual network activity detected

nl: This is for numbering the the lines in the text files.

labex:project/ $ nl config.txt 
     1    # Server Configuration
     2    port=8080
     3    max_connections=100

## To number all the lines including the blank one. 
## -b is for numbering the 'body'
## a is for 'all'
labex:project/ $ nl -b a config.txt 
     1    # Server Configuration
     2    port=8080
     3    max_connections=100
     4    

##Example
##-n is used to specify the numbering format.
##r means right-aligned (which is actually the default).
##z means adding leading zeros.
labex:project/ $ nl -n rz config.txt
000001    # Server Configuration
000002    port=8080
000003    max_connections=100

diff: This will show the difference between 2 files. See the below example

labex:project/ $ diff file1 file2
1c1
< this is file1
---
> this is file2
## The below example is to see the difference between 2 entire directories.
labex:project/ $ diff -r ~/Desktop ~/Code 
Only in /home/labex/Desktop: code.desktop
Only in /home/labex/Desktop: gedit.desktop
Only in /home/labex/Desktop: gvim.desktop
Only in /home/labex/Desktop: xfce4-terminal.desktop
  • 1c1: This tells us that line 1 in the first file was changed compared to line 1 in the second file. The format is line_number_in_file1 + operation + line_number_in_file2. The "c" stands for "change".

  • < this is file1: The < symbol shows content from the first file (file1). This is what was in the original file.

  • ---: This is just a separator line that divides the "before" and "after" content.

  • > this is file2: The > symbol shows content from the second file (file2). This is what the content changed to.