Essential File Management Tools
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
Using Links
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
- Use
tarto 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. - List the contents of one of the archives
- Create a symbolic link to one of the new archives in the /tmp directory.
- Move the symbolic link to ~/tmp/. Ensure the symbolic link still points to the correct location.
- Extract one of the archives contents into ~/tmp
- Remove the archive from your home directory. What happens to the symbolic link?
- Remove ~/tmp and all the archives
Solution
- -
tar -cvzf archive.tar.gz /etc /opt tar -cvjf archive.tar.bz2 /etc /opt tar -cvJf archive.tar.xz /etc /opt tar -tf archive.tar.gzln -s $(pwd)/archive.tar.gz /tmpmv /tmp/archive.tar.gz ~/tmptar -C tmp -xf archive.tar.gzrm ~/tmp/archive.tar.gzrm -rf ~/tmp; rm archive*
- Clint Jordan