Practice Exercise: Comparing Files and Identifying File Types
Objectives
- Learn how to compare files to find differences.
- Understand how to identify different file types.
- Gain hands-on experience using Linux commands for file comparison and type identification.
Scenario
As a Linux enthusiast, you want to become proficient in comparing files to find differences and identifying various file types on a Linux system. This exercise will guide you through the process of comparing files and recognizing file types using essential Linux commands.
Tasks
Task 1: Comparing Text Files
- Create two text files,
file1.txt
andfile2.txt
, with some sample text in each. - Use the
diff
command to compare the contents offile1.txt
andfile2.txt
. - Note any differences displayed by
diff
.[intern@intern-a1t-inf-lnx1 ~]$ cat file1.txt This line is similar This line is different 49KditpKMhuOTQ== [intern@intern-a1t-inf-lnx1 ~]$ cat file2.txt This line is similar This line is different i2BP9aUkUZ8NLg== [intern@intern-a1t-inf-lnx1 ~]$ diff file1.txt file2.txt 2c2 < This line is different 49KditpKMhuOTQ== --- > This line is different i2BP9aUkUZ8NLg==
- As you can see it didn't output the similar line
Task 2: Identifying File Types
- List the contents of a directory on your Linux system.
- Identify at least five different types of files (e.g., text files, directories, executables, symbolic links, etc.) within the directory.
- Use the
file
command to confirm the file types you identified. - Make a list of the identified file types and their descriptions.
# Create smaple empty file and directory [intern@intern-a1t-inf-lnx1 ~]$ touch file1 [intern@intern-a1t-inf-lnx1 ~]$ mkdir dir1 [intern@intern-a1t-inf-lnx1 ~]$ file file1 file1: empty [intern@intern-a1t-inf-lnx1 ~]$ file dir1 dir1: directory # Check symbolic (soft link) file [intern@intern-a1t-inf-lnx1 ~]$ ls -l /etc/yum.conf lrwxrwxrwx. 1 root root 12 Jan 12 2023 /etc/yum.conf -> dnf/dnf.conf [intern@intern-a1t-inf-lnx1 ~]$ file /etc/yum.conf /etc/yum.conf: symbolic link to dnf/dnf.conf # Check file with text content [intern@intern-a1t-inf-lnx1 ~]$ file /etc/passwd /etc/passwd: ASCII text [intern@intern-a1t-inf-lnx1 ~]$ file /etc/sysctl.conf /etc/sysctl.conf: ASCII text # Check binary (executable) file [intern@intern-a1t-inf-lnx1 ~]$ which cp /usr/bin/cp [intern@intern-a1t-inf-lnx1 ~]$ file /usr/bin/cp /usr/bin/cp: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=cb11f66fba73c02b5b6211fd81d4f9b01c99c444, stripped # Check block device file [intern@intern-a1t-inf-lnx1 ~]$ ls -l /dev/sd* brw-rw---- 1 root disk 8, 0 Oct 16 04:19 /dev/sda brw-rw---- 1 root disk 8, 16 Oct 16 04:19 /dev/sdb brw-rw---- 1 root disk 8, 32 Oct 16 04:19 /dev/sdc [intern@intern-a1t-inf-lnx1 ~]$ file /dev/sda /dev/sda: block special (8/0)
Task 3: Comparing Binary Files
- Create two binary files,
binary1.bin
andbinary2.bin
, by copying a binary executable (e.g.,/bin/ls
) to two different names. - Use the
cmp
command to comparebinary1.bin
andbinary2.bin
. - Note if there are any differences reported by
cmp
. - Try to compare the binaries
/bin/sh
and/bin/bash
- Note if there are any differences
[intern@intern-a1t-inf-lnx1 ~]$ cp /bin/ls binary1.bin
[intern@intern-a1t-inf-lnx1 ~]$ cp /bin/ls binary2.bin
[intern@intern-a1t-inf-lnx1 ~]$ cmp binary1.bin binary2.bin
[intern@intern-a1t-inf-lnx1 ~]$
[intern@intern-a1t-inf-lnx1 ~]$ cp /bin/sh sh.bin
[intern@intern-a1t-inf-lnx1 ~]$ cp /bin/bash bash.bin
[intern@intern-a1t-inf-lnx1 ~]$ cmp sh.bin bash.bin
sh.bin bash.bin differ: char 22, line 1
Task 4: Identifying Unknown Files
- Locate an unknown file (e.g., a file without a file extension or with an unfamiliar extension) on your system.
- Use the
file
command to identify the file type. - Research the identified file type to understand its purpose or use.
[intern@intern-a1t-inf-lnx1 ~]$ file binary1.bin
binary1.bin: Mach-O universal binary with 2 architectures: [x86_64:Mach-O 64-bit executable x86_64] [arm64e:Mach-O 64-bit executable arm64e]
binary1.bin (for architecture x86_64): Mach-O 64-bit executable x86_64
binary1.bin (for architecture arm64e): Mach-O 64-bit executable arm64e
Task 5: Using Wildcards
- Create a directory with several files of different types, including text files, executables, and directories.
- Use wildcards (e.g.,
*
or?
) with commands likels
orfile
to identify specific types of files within the directory. - List all text files, executables, and directories separately using wildcards.
[intern@intern-a1t-inf-lnx1 ~]$ ls *.txt
NA.txt file2.txt notebooks-data.txt test2.txt
amp_workspaces.txt file3.txt output.txt test3.txt
config-diff.txt intern_testing_file.txt test-ldap.txt usmsc30c.txt
env.txt less_than11.txt test-sed.txt
file1.txt lms.txt test1.txt
[intern@intern-a1t-inf-lnx1 ~]$ ls file?.txt
file1.txt file2.txt file3.txt
[intern@intern-a1t-inf-lnx1 ~]$ ls *.txt
NA.txt file2.txt notebooks-data.txt test2.txt
amp_workspaces.txt file3.txt output.txt test3.txt
config-diff.txt intern_testing_file.txt test-ldap.txt usmsc30c.txt
env.txt less_than11.txt test-sed.txt
file1.txt lms.txt test1.txt
[intern@intern-a1t-inf-lnx1 ~]$ ls file?.txt
file1.txt file2.txt file3.txt
Conclusion
This practice exercise has allowed you to practice comparing text and binary files to find differences and identifying various file types on a Linux system. You've used essential commands like diff
, file
, and cmp
to perform these tasks. Understanding how to compare files and recognize their types is essential for efficient file management and system administration in Linux.