Skip to content
Laviyx Learn

Try

Linux Commands Practice

Practise everyday Linux commands: files, searching, pipes, text tools, permissions and processes. Every solution has been run on Ubuntu and shows its real output.

Beginner14 problemsUpdated

Review first: Creating, Copying and Deleting Files, Reading Files, Pipes and Redirection, Searching Text with grep, Finding Files with find, sort, uniq, cut, tr and wc, Editing Text with sed, Working with Columns in awk, File Permissions, Processes

  1. 1.Create a project layout

    Beginner

    With one mkdir command, create site/css, site/js and site/images. Then print the folder names inside site.

    Show hint

    Use mkdir -p with braces.

    Show solution
    Terminal
    mkdir -p site/{css,js,images}
    ls site
    Output
    css
    images
    js
  2. 2.Copy and rename

    Beginner

    Copy report.txt to archive/report-2026.txt, creating archive first, then delete the original and list archive.

    Starter code (Terminal)
    echo "Sales" > report.txt
    Show hint

    mkdir -p, cp and rm.

    Show solution
    Terminal
    mkdir -p archive
    cp report.txt archive/report-2026.txt
    rm report.txt
    ls archive
    Output
    report-2026.txt
  3. 3.Count errors in a log

    Beginner

    Print how many lines of app.log contain ERROR.

    Starter code (Terminal)
    cat > app.log <<'EOF'
    09:00 INFO Server started
    09:01 INFO User asha logged in
    09:02 WARN Disk 80 percent full
    09:03 INFO User ravi logged in
    09:04 ERROR Payment failed
    09:05 INFO Report created
    09:06 ERROR Database timeout
    09:07 INFO User asha logged out
    EOF
    Show hint

    grep -c.

    Show solution
    Terminal
    grep -c ERROR app.log
    Output
    2
  4. 4.Hide normal messages

    Beginner

    Print all the lines of app.log except those with INFO.

    Starter code (Terminal)
    cat > app.log <<'EOF'
    09:00 INFO Server started
    09:01 INFO User asha logged in
    09:02 WARN Disk 80 percent full
    09:03 INFO User ravi logged in
    09:04 ERROR Payment failed
    09:05 INFO Report created
    09:06 ERROR Database timeout
    09:07 INFO User asha logged out
    EOF
    Show hint

    grep -v.

    Show solution
    Terminal
    grep -v INFO app.log
    Output
    09:02 WARN Disk 80 percent full
    09:04 ERROR Payment failed
    09:06 ERROR Database timeout
  5. 5.Print certain lines

    Beginner

    Print only lines 3 to 5 of app.log.

    Starter code (Terminal)
    cat > app.log <<'EOF'
    09:00 INFO Server started
    09:01 INFO User asha logged in
    09:02 WARN Disk 80 percent full
    09:03 INFO User ravi logged in
    09:04 ERROR Payment failed
    09:05 INFO Report created
    09:06 ERROR Database timeout
    09:07 INFO User asha logged out
    EOF
    Show hint

    sed -n '3,5p', or head with tail.

    Show solution
    Terminal
    sed -n '3,5p' app.log
    Output
    09:02 WARN Disk 80 percent full
    09:03 INFO User ravi logged in
    09:04 ERROR Payment failed
  6. 6.Find old log files

    Intermediate

    Under app, print the .log files that were last changed more than 5 days ago.

    Starter code (Terminal)
    mkdir -p app/src app/logs
    echo "print('hi')" > app/src/main.py
    echo "x" > app/src/util.py
    echo "old" > app/logs/old.log
    echo "new" > app/logs/new.log
    touch -d "10 days ago" app/logs/old.log
    Show hint

    find with -name and -mtime.

    Show solution
    Terminal
    find app -name "*.log" -mtime +5
    Output
    app/logs/old.log
  7. 7.Count lines in Python files

    Intermediate

    Print the total number of lines in all the .py files under app.

    Starter code (Terminal)
    mkdir -p app/src app/logs
    echo "print('hi')" > app/src/main.py
    echo "x" > app/src/util.py
    echo "old" > app/logs/old.log
    echo "new" > app/logs/new.log
    touch -d "10 days ago" app/logs/old.log
    Show hint

    find ... -exec cat {} +, then wc -l.

    Show solution
    Terminal
    find app -name "*.py" -exec cat {} + | wc -l
    Output
    2
  8. 8.Highest salaries

    Intermediate

    Print the names and salaries of the three highest-paid people in staff.csv.

    Starter code (Terminal)
    cat > staff.csv <<'EOF'
    name,city,department,salary
    Asha,Hyderabad,Engineering,90000
    Ravi,Pune,Sales,55000
    Meena,Hyderabad,Engineering,105000
    Karan,Chennai,Support,42000
    Divya,Pune,Engineering,88000
    Arjun,Hyderabad,Sales,61000
    EOF
    Show hint

    Skip the header, sort by the 4th column, and take the first three.

    Show solution
    Terminal
    tail -n +2 staff.csv | sort -t, -k4 -nr | head -n 3 | cut -d, -f1,4
    Output
    Meena,105000
    Asha,90000
    Divya,88000
  9. 9.People per city

    Intermediate

    Print how many people are in each city, largest first.

    Starter code (Terminal)
    cat > staff.csv <<'EOF'
    name,city,department,salary
    Asha,Hyderabad,Engineering,90000
    Ravi,Pune,Sales,55000
    Meena,Hyderabad,Engineering,105000
    Karan,Chennai,Support,42000
    Divya,Pune,Engineering,88000
    Arjun,Hyderabad,Sales,61000
    EOF
    Show hint

    cut, sort, uniq -c, then sort -nr.

    Show solution
    Terminal
    tail -n +2 staff.csv | cut -d, -f2 | sort | uniq -c | sort -nr
    Output
          3 Hyderabad
          2 Pune
          1 Chennai
  10. 10.Total salary by department

    Intermediate

    Print the total salary for each department, sorted by name.

    Starter code (Terminal)
    cat > staff.csv <<'EOF'
    name,city,department,salary
    Asha,Hyderabad,Engineering,90000
    Ravi,Pune,Sales,55000
    Meena,Hyderabad,Engineering,105000
    Karan,Chennai,Support,42000
    Divya,Pune,Engineering,88000
    Arjun,Hyderabad,Sales,61000
    EOF
    Show hint

    An awk array indexed by $3.

    Show solution
    Terminal
    awk -F, 'NR > 1 {t[$3] += $4} END {for (d in t) print d, t[d]}' staff.csv | sort
    Output
    Engineering 283000
    Sales 116000
    Support 42000
  11. 11.Change a setting

    Beginner

    In app.conf, change port=8080 to port=9090 in place, and print the file.

    Starter code (Terminal)
    printf "host=localhost\nport=8080\n" > app.conf
    Show hint

    sed -i.

    Show solution
    Terminal
    sed -i 's/port=8080/port=9090/' app.conf
    cat app.conf
    Output
    host=localhost
    port=9090
  12. 12.Fix permissions

    Beginner

    Make deploy.sh executable by its owner and readable by everyone else, and print the mode as a number.

    Starter code (Terminal)
    echo "echo hi" > deploy.sh
    chmod 644 deploy.sh
    Show hint

    chmod 744, and stat -c %a.

    Show solution
    Terminal
    chmod 744 deploy.sh
    stat -c '%a' deploy.sh
    ./deploy.sh
    Output
    744
    hi
  13. 13.Save output and errors

    Intermediate

    Run ls /etc/hostname /nothing, sending normal output to ok.txt and errors to bad.txt. Print the number of lines in each file.

    Show hint

    > and 2>.

    Show solution
    Terminal
    ls /etc/hostname /nothing > ok.txt 2> bad.txt
    wc -l ok.txt bad.txt | head -n 2
    Output
     1 ok.txt
     1 bad.txt
  14. 14.Start and stop a job

    Intermediate

    Start sleep 60 in the background, print running if pgrep -c sleep finds it, stop it, and print the new count.

    Show hint

    $! is the PID of the background job.

    Show solution
    Terminal
    sleep 60 &
    [[ $(pgrep -c sleep) -ge 1 ]] && echo running
    kill $!
    sleep 0.2
    pgrep -c sleep
    Output
    running
    0