Manage Find Grep Tar link

TIPS & TRICKS

To search the contents of the man pages

# man -k searchterm 
# man -K searchterm

Manage Find

To keep in mind.
Execute a simply find command in order to display the result in the terminal:

# find / -user harry

If it seems ok, you can use exec <cmd> {} <target> \; after:

# find / -user harry -exec cp -rf {} /root/find.user \;

Manage Find 1

| Find all files owned by harry and copy them to /root/find.user
| Find files (not subdirectories) owner by user natasha and copy in /root/findresults

# find / -user harry -exec cp -rf {} /root/find.user \;
# find / -maxdepth 1 -user natasha -type f -exec cp {} /root/findresults \;

Find the files 2

| Find All Files (not directory) in /home (not subdirectories) owned by harry and copy them to /tmp.
| Find All directory in /home (not subdirectories) owned by harry and copy them to /tmp/tmpdir.

# find /home -maxdepth 1 -user harry -type f -exec cp {} /tmp \;
# find /home -maxdepth 1 -user harry -type d -exec cp -r {} /tmp/tmpdir \;

Find the files 2

| Find all files owned by harry in /home expect files that began by “.” and copy them to /tmp/harryTMP/
| Find all directory owned by harry in /home expect files that began by “.” and copy them to /tmp/harryTMP2/

# find /home -user harry -not -path "/home/*/.*" -type f -exec cp {} /tmp/harryTMP/ \;
# find /home -user harry -not -path "/home/*/.*" -type d -exec cp -r {} /tmp/harryTMP2/ \;

Manage Find 3

| Find All Files in /etc (not subdirectories) that where modified more than 180 days ago.
| And copy all of them to a directory /var/tmp/old

# find /etc -type f -maxdepth 1 -mtime +180 -exec cp {} /var/tmp/old \;

Manage Find 4

| Find all setuid files on the system and save the list.
| Find all setgid files on the system and save the list.

# find / -type f -perm -4000 > setuid_list
# find / -type f -perm -2000 > setgid_list

Find the files 5

| (1) Find All Files in /etc (not subdirectories) that contain in the name text “chrony” (ignore case).
| (2) Find All Files in /etc (not subdirectories) that contain text “chrony” (ignore case).
| Copy output in files.

# find /etc  -maxdepth 1 -name *chrony* > MyFile.txt  // (1)
# grep -i "chrony" /etc/* 2>/dev/null > MyFile.txt // (2) Grep don't go more deep than /etc/

Utilisation des commandes find et grep

| Trouvez tous les fichiers .log dans /var/log contenant le mot ERROR
| Enregistrez les résultats dans un fichier /backup/errors.log

Le “+“ est très important ici.

# find /var/log -name "*.log" -exec grep -e "ERROR" {} + > /backup/errors.log

Manage Grep

Manage Grep 1

| Find the rows that contain abcde from file /etc/testfile, and write it to the file/tmp/testfile, and the sequence is requested as the same as /etc/testfile.
| Search the string sarah in the /etc/passwd file and save the output in /root/lines

# grep abcde /etc/testfile > /tmp/testfile
# grep sarah /etc/passwd > /root/lines

Manage Grep 2

| Finds all lines in the file /usr/share/xml/iso-codes/iso_639_3.xml that contain the string ng.
| Put a copy of all these lines in the file /root/list in their original order.
| The /root/list file must not contain empty lines, and all lines must be exact copies of the original lines in /usr/share/xml/iso-codes/iso_639_3.xml.

# grep ng /usr/share/xml/iso-codes/iso_639_3.xml | grep -v ^$ > /root/list

Manage Grep 3

| Remove all the empty line and the word “Unknow” in file01.txt and copy the result in FinalFile.txt

# grep -v ^$ file01.txt | grep -v "Unknow" > FinalFile.txt

Manage Grep 4

| Perform a case-insensitive search for all lines in the /usr/share/dict/linux.words file that begin with the pattern “essential”.
| Redirect the output to /tmp/pattern.txt file.
| Make sure that empty lines are omitted.

# grep -e '^essential' /usr/share/dict/linux.words | grep -v '^$' 

Manage Tar

To keep in mind.
Tar:

# tar -cvjf /path/detination.tar.bz2 /path/source/to/tar
# tar -cvzf /path/detination.tar.gz /path/source/to/tar

Untar:

# tar xvjf detination.tar.bz2
# tar xvzf detination.tar.gz

Manage Tar 1

| Create an archive file - Backup the /var/tmp directory as /root/test.tar.gz
| Create the archive file /root/local.tgz for /usr/local compressed by gzip.

# tar -cvzf /root/test.tar.gz /var/tmp 
# tar -cvzf /root/local.tar.gz /usr/local

Manage Tar 2

| Create a backup file named /root/backup.tar.bz2, which contains the contents of /usr/local in /root/test directory
| You must use the bzip2 compression.

# mkdir -p /root/test
# tar -cvjf backup.tar.bz2 /usr/local -C /root/test

Manage Grep and Tar

| Find All Log Messages in /var/log/messages That Contain “ACPI”, and Export Them to a File Called /root/logs.
| Then Archive All of /var/log and Save It to /tmp/log_archive.tgz.

# grep ACPI /var/log/messages >> /root/logs

No compression is needed, so we use only cvf options:

# tar cvf /tmp/log_archive.tgz /var/log/ 

| Create an empty file hard1 under /tmp and display its attributes.
| Create hard links hard2 and hard3. Edit hard2 and observe the attributes.
| Remove hard1 and hard3 and list the attributes again

# cd /tmp
# touch hard1
# ls -li hard1

# ln hard1 hard2
# ln hard1 hard3

# echo "Redhat" > hard2
# ls -li // They have the same inode
# rm -f hard{1,3}
# ls -li

| Create an empty file soft1 under /root pointing to /tmp/hard2.
| Edit soft1 and list the attributes after editing. Remove hard2 and then list soft1

# ln -s /tmp/hard2 /root/soft1
# ls -li /root/soft1 // also they've the same inode
# echo "from soft1" >> /root/soft1
# ls -li soft1 /tmp/hard2
# rm -f /tmp/hard2 // The link is broken

Documentations

Internet
Git
ChatGPT

> Partager <