Essential File Management Tools

Oct 31, 2025

Filesystem Hierarchy Standard

Filesystem Hierarchy Standard

man hier

Finding Files

Look for binaries in $PATH

which grep

Find using name

find / -name "hosts"

Find using type and size

find / -type f -size +100M

Find using multiple parameters

find / -type f -size +100M

Find using or -o

find / -type f -name '*c' -o -name '*d'

Embedding commands in find command

find /etc -exec grep -l student {} \; 2> /dev/null

Where {} refers to the results of the find command. grep -l outputs the files that contain the search string rather than the lines. \; ends the embedded command, the leading space is required.

Embedding multiple commands

mkdir ~/find/results
find /etc -exec grep -l student {} \; -exec cp {} ~/find/results \; 2> /dev/null

Using xargs to execute commands

sudo find /etc -name '*' -type f | sudo xargs grep '127.0.0.1'

or

sudo sh -c "find /etc -name '*' -type f | xargs grep 127.0.0.1"

or

sudo bash -c "find /etc -name '*' -type f | xargs grep 127.0.0.1"

Mounting Filesystems

List block devices

lsblk

Mount a block device

mount /dev/{device-name} /mnt

Hard link (second name that points to the same inode).

  • Hard links must be on the same device
  • Directories cannot be hard linked
ln {source} {destination}

Symbolic Link (points to a name)

ln -s {source} {destination}
  • Always use absolute paths so link survives being moved
  • Soft links can be on different devices than what they point to

Archiving Files

tar -> tape archiver

  • By default, it doesn’t compress data

Archive multiple directories

tar -cvf archive.tar /home /etc

Show contents of an archive

tar -tvf archive.tar

Extract contents of an archive to the current directory

tar -xvf archive.tar

Compress with gzip

tar -czvf archive.tar.gz /home /etc

Compress with bzip2

tar -cjvf archive.tar.bz2 /home /etc

Compress with xz

tar -cJvf archive.tar.xz /home /etc

Determine compression method, if any, of ambiguous extension

file archive.tar

Using Compression

  • gzip (-z) is still the most common compression utility (fastest)
  • bzip2 (-j) better compression than gzip, but slower
  • xz (-J) best compression, but much slower

Lab Exercise

  1. Use tar to create compressed archives in ~/ containing all files in the /etc and /opt directories using gzip, bzip2, and xz. Note the difference in time required to create the archives and the resulting sizes.
  2. List the contents of one of the archives
  3. Create a symbolic link to one of the new archives in the /tmp directory.
  4. Move the symbolic link to ~/tmp/. Ensure the symbolic link still points to the correct location.
  5. Extract one of the archives contents into ~/tmp
  6. Remove the archive from your home directory. What happens to the symbolic link?
  7. Remove ~/tmp and all the archives
Solution
  1. -
    tar -cvzf archive.tar.gz /etc /opt
    tar -cvjf archive.tar.bz2 /etc /opt
    tar -cvJf archive.tar.xz /etc /opt
  2. tar -tf archive.tar.gz
  3. ln -s $(pwd)/archive.tar.gz /tmp
  4. mv /tmp/archive.tar.gz ~/tmp
  5. tar -C tmp -xf archive.tar.gz
  6. rm ~/tmp/archive.tar.gz
  7. rm -rf ~/tmp; rm archive*

- Clint Jordan