Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Practical Linux Command Exercises for File and Directory Management

Tech 1

1. List All Files in the /etc/ Directory

Use the ls command to view all file information in the /etc/ diretcory.

Common ls options:

  • -l: Display file information in long format
  • -A: Show all files, including hidden files starting with .
  • -a: Show all files, including . and ..
  • -d: Display properties of the directory itself (not its contents)
  • -h: Provide human-readable size units (K, M, etc.)
  • -s: Show disk space usage for each file
$ ls -la /etc

2. List Files or Directories Containing the Letter "a" in /etc/

$ ls -d /etc/*a*/

3. List Files Ending with ".conf" in /etc/

$ ls -l /etc/*.conf

4. List Files Starting with "y" in /etc/

$ ls -l /etc/y*

5. Find Files with ".log" Extension in /var/ Directory

Basic find syntax: find [path] [option value]

  • -name: Search files by name (supports wildcards like *)
  • -type f: Regular files
  • -type d: Directories
$ find /var/ -name "*.log" -type f

6. Create a Test Directory in /opt

$ mkdir /opt/test

7. Create Five Files in the Test Directory

$ cd /opt/test
$ touch abc.txt def.txt ghi.txt xxx.txt yyy.txt
$ ls
abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt

8. Modify the Last Modification Times of the Five Files

Use stat to get file time information:

$ stat /opt/test/abc.txt

Modify or set the modification time:

$ touch -m -d "2024-07-15 00:00" /opt/test/abc.txt
$ touch -m -d "2024-07-14 00:00" /opt/test/def.txt
$ touch -m -d "2024-07-13 00:00" /opt/test/ghi.txt
$ touch -m -d "2024-07-12 00:00" /opt/test/xxx.txt
$ touch -m -d "2024-07-11 00:00" /opt/test/yyy.txt

9. Create Directory a in the Test Directory

$ mkdir /opt/a
$ ls /opt
a  test

10. Copy the Five Files to Directory a

cp — Copy files Format: cp [options] source_file... target_path Common option:

  • -r: Recursive (required for copying directories)
$ cp -r abc.txt def.txt ghi.txt xxx.txt yyy.txt /opt/a/
$ ls
abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt
$ cd /opt/a
$ ls
abc.txt  def.txt  ghi.txt  xxx.txt  yyy.txt

11. Create a Compressed Archive of Directory a in the Home Directory

tar creates compressed archives with minimal disk space usage. Compression formats:

  • .gz: gzip format (fast compression)
  • .bz2: bzip2 format
  • .xz: xz format (smaller size)

Compress: tar [options] /path/archive_name /files_to_compress... Extract: tar [options] /path/archive [options] /target_location

Common tar options:

  • -c: Create archive
  • -x: Extract archive
  • -f: Specify archive filename
  • -z, -j, -J: Use gzip, bzip2, or xz compression
  • -t: List archive contents
  • -C: Specify extraction directory
$ tar -zcf /home/bak.tar.gz .
$ ls /home
bak.tar.gz

12. Delete Files Older Than 3 Days in the Test Directory

Search by modification time: find path -mtime -days/+days

  • -mtime: Search by last modification time
  • +: Files older than specified days
  • -: Files within specified days
  • -exec rm -rf {} \;: Execute rm -rf on each found file
$ find /opt/test -type f -mtime +3 -exec rm -f {} \;
$ ls /opt/test
abc.txt  def.txt  ghi.txt

13. Delete Files Within 3 Days in /opt Directory

$ find . -type f -mtime -3 -exec rm -f {} \;  # . represents current directory
$ ls  # a and test are directories, not files
a  test

14. Delete Files Exactly 3 Days Old

$ find /opt/test -type f -mtime 2 -exec rm -f {} \;  # -mtime 2 selects files modified exactly 3 days ago
$ ls /opt/test
ghi.txt

15. Copy Files from /opt/test/a to /opt/test/

$ find /opt/test/a -type f -exec cp --parents \{\} /opt/test \;  # Copy files while preserving directory structure

16. Create Directory /opt/test0

$ mkdir /opt/test0

17. Create Three Files with Specific Sizes in /opt/test0/

Use dd to create test files of specified sizes. Syntax: dd if=/dev/zero of=filename bs=size count=1

  • if: Input file
  • of: Output file
  • bs: Block size in bytes
  • count: Number of blocks to copy
  • /dev/zero: Character device that returns zero bytes
$ cd test0
$ dd if=/dev/zero of=a.mp4 bs=5M count=1
$ dd if=/dev/zero of=b.mp4 bs=20M count=1
$ dd if=/dev/zero of=c.mp4 bs=80M count=1
$ ls
a.mp4  b.mp4  c.mp4

18. Create Directory /opt/test0/b/

$ mkdir b
$ ls
a.mp4  b  b.mp4  c.mp4

19. Copy Files from /opt/test0/ to /opt/test0/b/

$ cp -r a.mp4 b.mp4 c.mp4 /opt/test0/b/
$ ls /opt/test0/b/
a.mp4  b.mp4  c.mp4

20. Find and Delete Files Larger Than 20M in /opt/test0/

Search by file size: find path -size size [units K M G]

  • size: Files equal to size
  • -size: Files in range [0, size)
  • +size: Files in range (size, infinity)
$ find /opt/test0/ -size +20M -exec rm -rf {} \;
$ ls
a.mp4  b  b.mp4

21. Find and Delete Files Smaller Than 20M in /opt/test0/

$ find /opt/test0/ -type f -size -20M -delete
$ ls
b  b.mp4

22. Find and Delete Files Exactly 20M in /opt/test0/

$ find /opt/test0/ -size 20M -exec rm -rf {} \;
$ ls
b

23. Copy Files from /opt/test0/b to /opt/test0

$ cp a.mp4 b.mp4 c.mp4 /opt/test0
$ cd /opt/test0
$ ls
a.mp4  b  b.mp4  c.mp4

24. Open a New Virtual Machine

25. Upload bak.tar.gz from Home Directory to New Machine's /opt

Use scp to transfer files. Syntax: scp [options] username@host:/remote_path local_path

$ scp root@192.168.2.4:/home/bak.tar.gz /opt/
Are you sure you want to continue connecting (yes/no)? yes
root@192.168.2.4's password:
bak.tar.gz                    100%  177   108.1KB/s   00:00
$ ls /opt
bak.tar.gz

26. Download /etc/skel/ Directory from New Machine to Local /opt

Add -r option for recursive directory copying.

$ scp -r root@192.168.2.3:/etc/skel/ /opt/
root@192.168.2.3's password:
.bash_logout                  100%   18     7.4KB/s   00:00
.bash_profile                 100%  193   105.1KB/s   00:00
.bashrc                       100%  231   125.2KB/s   00:00
$ ls /opt
a  skel  test  test0

27. Schedule Weekly Compression of .repo Files with Timestamp

Use crontab for scheduling tasks. Options:

  • -l: List current user's scheduled tasks
  • -e: Edit scheduled tasks

Crontab format: minute hour day month weekday command_path (0 and 7 both represent Sunday)

$ crontab -e
0 0 * * 3 /usr/bin/tar -zcvf /tmp/yum-$(date "+\%Y\%m\%d\%H\%M\%S").tar.gz /etc/yum.repos.d/*.repo
crontab: installing new crontab
$ ls -l /tmp/
total 0

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.