0% found this document useful (0 votes)
61 views

Lesson 03 Essential File Management Tools

This document discusses essential file management tools in Linux including commands to find, move, copy, archive and compress files. It covers commands like ls, mkdir, cp, mv, rmdir, rm, locate, find, mount, links (hard vs soft), tar for archiving, and compression tools like gzip, bzip2 and xz. The document provides examples and explanations of how to use each tool effectively.

Uploaded by

Taha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Lesson 03 Essential File Management Tools

This document discusses essential file management tools in Linux including commands to find, move, copy, archive and compress files. It covers commands like ls, mkdir, cp, mv, rmdir, rm, locate, find, mount, links (hard vs soft), tar for archiving, and compression tools like gzip, bzip2 and xz. The document provides examples and explanations of how to use each tool effectively.

Uploaded by

Taha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lesson 03 Essential File

Management Tools
Created @December 7, 2021 9:01 AM

Class

Type

Materials

Reviewed

Last Update @December 8, 2021 6:37 AM


3.1 Essential File Management Tasks
3.2 Finding Files
3.3 mount
3.4 Links
3.5 Working with Links
3.6 tar Tape Archiver
Compressed Files

3.1 Essential File Management Tasks


ls
mkdir
// -p, --parents no error if existing, make parent directories as needed
mkdir -p /new/scripts
cp --help | less
cp -r /etc/h* . // copy recursively, directories and their contents
mv
rmdir
rm -rf

3.2 Finding Files


which // looks for binaries in the $PATH
echo $PATH
/home/student/.local/bin:/home/student/bin:/usr/local/bin:/usr/bin:/usr/local/sbi

Lesson 03 Essential File Management Tools 1


n:/usr/sbin
locate // uses a database, built by updatedb to find files in database.
// if new file is created, you need to update database.
find // best, find files based on many criteria
find / -name "hosts"
find / -name "host" // no result!
find / -type f -size +100M
find /tmp -type d // find directories only, no files
find / -user student // if the user is logged in, many files will be owned and opend
by that usr, so the result will be many files.

grep -l student /etc/* 2> /dev/null // search for the tex


t "student" in "/etc/*"
find /etc -exec grep -l student {} \;
find /etc -exec grep -l student {} \; 2> /dev/null
find /etc -size +100c -exec grep -l student {} \; 2> /dev/null // +100c: more than 1
00Byte, -100c: less than 100Byte
find /etc -size +100c -exec grep -l student {} \; -exec cp {} /tmp \; 2> /dev/null
find /etc -size -1000c -exec cp {} pictures \;

cp /etc/[a-c]* /tmp/files // copy files that start with a, b, or c


mv [ab]* photos/ // move files that start with a or b

# locate useradd
locate: can not stat () `/var/lib/mlocate/mlocate.db': No such file or directory

# updatedb // update database

# locate useradd
/etc/default/useradd
/usr/sbin/luseradd
/usr/sbin/useradd
/usr/share/bash-completion/completions/luseradd
/usr/share/bash-completion/completions/useradd
/usr/share/man/de/man8/useradd.8.gz
/usr/share/man/fr/man8/useradd.8.gz
/usr/share/man/id/man8/useradd.8.gz
/usr/share/man/it/man8/useradd.8.gz
/usr/share/man/ja/man8/useradd.8.gz
/usr/share/man/man1/luseradd.1.gz
/usr/share/man/man8/useradd.8.gz
/usr/share/man/ru/man8/useradd.8.gz
/usr/share/man/tr/man8/useradd.8.gz
/usr/share/man/zh_CN/man8/useradd.8.gz
/usr/share/man/zh_TW/man8/useradd.8.gz

3.3 mount
To access a device, it must be connected to a directory (mounting the device).

Linux uses multiple mounts, /, boot, etc, ....

Lesson 03 Essential File Management Tools 2


Different types of data are on different devices, for different reasons:

Security

Manageability

Specific mount options

3.4 Links
Links are pointers to files in different locations.
links are useful to make the same file available on different locations.

Linux uses Hard Links and Symbolic Links.

Hard Links: created with ln .

Symbolic Links: created with ln -s .

Lesson 03 Essential File Management Tools 3


Soft Link (symlink) Hard Link

3.5 Working with Links


(ln works like cp source destination )

ls -il // i: display the Inode number


$ ls -il
total 4
33579936 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Desktop
33579937 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Documents
52983004 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Downloads
52983006 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Music
897067 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Pictures
18008804 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Public
897066 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Templates
2352279 drwxrwxr-x. 2 student student 4096 Dec 7 04:45 test
18008805 drwxr-xr-x. 2 student student 6 Dec 4 23:46 Videos

# ls -il /etc/hosts
17353158 -rw-r--r--. 1 root root 158 Sep 10 2018 /etc/hosts

// Hard Links
# ln /etc/hosts /root/hardhosts // hard link
# ls -il /etc/hosts /root/hardhosts
17353158 -rw-r--r--. 2 root root 158 Sep 10 2018 /etc/hosts // same inode
17353158 -rw-r--r--. 2 root root 158 Sep 10 2018 /root/hardhosts // same inode

// Symbolic Links
# ln -s /etc/hosts symhosts //
Symbolic link
# ls -il /etc/hosts /root/hardhosts /root/symhosts
//Original file
17353158 -rw-r--r--. 2 root root 158 Sep 10 2018 /etc/hosts
//hard link//1-same inode//2-date as the original file
17353158 -rw-r--r--. 2 root root 158 Sep 10 2018 /root/hardhosts
//Symbolic link//1-different inode//2-date is link creation time//3-Size is differ
ent, only 10 Bytes//4-Permissions are completely open (but the effective permissions a
re on the original file).

Lesson 03 Essential File Management Tools 4


33578968 lrwxrwxrwx. 1 root root 10 Dec 7 12:08 /root/symhosts -> /etc/hosts
ln -s /var . // symbolic link for a directory // ls -l ---> var -> /var

// remove the original file


# rm -f /etc/hosts
# ls -il /etc/hosts /root/hardhosts /root/symhosts
ls: cannot access '/etc/hosts': No such file or directory
17353158 -rw-r--r--. 1 root root 158 Sep 10 2018 /root/hardhosts
33578968 lrwxrwxrwx. 1 root root 10 Dec 7 12:08 /root/symhosts -> /etc/hosts //
red background means Broken Link

# ls
anaconda-ks.cfg hardhosts initial-setup-ks.cfg symhosts // red means Broken Lin
k

# cat symhosts // symbolic link original file is deleted


cat: symhosts: No such file or directory // could be fixed using the Hard Link file
hardhosts
# cat /root/hardhosts // hard link file data still exist
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

# ln /root/hardhosts /etc/hosts // using the Hard Link file "hardhosts"


to recover /etc/hosts
# ls -il /etc/hosts /root/hardhosts /root/symhosts
17353158 -rw-r--r--. 2 root root 158 Sep 10 2018 /etc/hosts
17353158 -rw-r--r--. 2 root root 158 Sep 10 2018 /root/hardhosts // same inode
33578968 lrwxrwxrwx. 1 root root 10 Dec 7 12:08 /root/symhosts -> /etc/hosts

💡 an absolute path specifies the location from the root directory, whereas
relative path is related to the current directory.

3.6 tar Tape Archiver

💡 By default, tar does NOT compress files.

tar -cvf my_archive.tar /home /etc //c: create v: verbose f: file


tar -tvf my_archive.tar | less // show archive contents
tar -xvf my_archive.tar // extract to the current directory
-C // switch the output path
-z, -j, -J // compression
tar xvf mytar.tar -C /tmp // exctract to /tmp instead of the current
directory
mkdir /tmp/archive; tar xvf mytar.tar.xz -C /tmp/archive

mv mytar.tar mytar // remove the extention

Lesson 03 Essential File Management Tools 5


# file mytar // chech file type
mytar: POSIX tar archive (GNU)

tar cJvf home.tar.xz /home

Compressed Files
gzip // gunzip

bzip2

zip

xz

gzip --help
gzip mytar.tar // it will be gzip.tar.gz // mytar.tar will not be their anymo
re
gunzip mytar.tar.gz // uncompress, the result mytar.tar // .gz will not be their a
nymore
gunzip -k mytar.tar.gz // -k to keep the original file

bzip2 --help
bzip2 -k mytar.tar // -k to keep the original file // the result mytar.tar.bz2

xz --help
xz -k mytar.tar // -k to keep the original file // slower, and higher compressi
on ration

ctrl-a : bring the cursor at the beginning of the command.

Lesson 03 Essential File Management Tools 6

You might also like