Note: When doing this project, After using text editor of your choice to create and access the file on insert mode.Ensure the first line is always having # !/bin/bash, then the second line is having the correct command/answer.
(From your terminal, convert the file created to SCRIPT
i.e: cdmod u+x 1-listit)
input: pwd
(print working directory)
input: ls
(list directory contents)
input: cd
(change directory)
input: ls -l
(list directory contents in long form)
4. Display current directory contents, including hidden files (starting with .). Use the long format.
input: ls -la
(list directory contents in long form, including hidden files)
input: ls -la
(Note: Are files inherently ordered?)
input: mkdir /tmp/my_first_directory
(Create a holberton directory inside the tmp directory)
input: mv /tmp/betty /tmp/my_first_directory/betty
(Move file betty, which is located inside the tmp directory, to the holberton directory, which is also located inside the tmp directory. This exercise required some dir traversing.)
input: rm /tmp/my_first_directory/betty
(Remove file betty located in tmp/my_first_directory directory.)
input: rmdir /tmp/my_first_directory
(Remove directory my_first_directory located in directory tmp.)
input: cd -
(Change directory to the previous directory you were in.)
11. Write a script that lists all files (even ones with names beginning with a period character, which are normally hidden) in the current directory and the parent of the working directory and the /boot directory (in this order), in long format.
*input: ls -la . .. /boot
(List all files/directories, including hidden files/directories, from 3 separate directories: current directory, parent of working directory, and /boot directory. The ls command allows multiple directories to be listed separated by spaces.)
12. Write a script that prints the type of the file named iamafile. The file iamafile will be in the /tmp directory when we will run your script.
input: file /tmp/iamafile
(Prints the type of file iamafile.)
13. Create a symbolic link to /bin/ls, named ls. The symbolic link should be created in the current working directory.
input: ln -s /bin/ls __ls__
(Create a symbolic link named ls for /bin/ls)
14. Create a script that copies all the HTML files from the current working directory to the parent of the working directory, but only copy files that did not exist in the parent of the working directory or were newer than the versions in the parent of the working directory.
input: cp -u *.html ..
*(Copy all html files from the current directory to the parent directory, but only copy files that didn't exist in the parent directory or are newer versions than the ones that already exist in the parent directory. The -u option didn't show on the terminal manual page. The -u option copies the file into the directory if its a newer version. If the file doesn't exist in the directory, it will copy over. The -n option works for copying files that don't exist in the parent directory, but it doesn't check if the file is a newer version or not.)
15. Create a script that moves all files beginning with an uppercase letter to the directory /tmp/u. You can assume that the directory /tmp/u will exist when we will run your script.
input: mv [[:upper:]]* /tmp/u
(Move all files that begin with a capital letter to /tmp/u)
16. Create a script that deletes all files in the current working directory that end with the character ~.
input: rm *~
(Deletes all files in the current directory that end with a ~)
17. Create a script that creates the directories welcome/, welcome/to/ and welcome/to/school in the current directory. You are only allowed to use two spaces (and lines) in your script, not more.
input: mkdir -p welcome/to/school
(Create directory welcome
in current directory. Create directory to
inside directory welcome
. Create directory holberton
inside directory to
. The -p option creates any intermediate directories in the path argument.)
18. Write a command that lists all the files and directories of the current directory, separated by commas (,) and hidden directory names(..) should end with a slash (/)
input: ls -map*
(List all files and directories of the current directory, separated by commas. Directory names should end with a /
. The listing should be alph ordered, except for dot (.) or dot dot (..), which should be listed at the beginning.)
The -a option is to show any hidden files.
The -p option writes a /
at the end of directory names.
The -m option streams the output, separating each listing with commas.
19. Create a magic file school.mgc that can be used with the command file to detect School data files. School data files always contain the string SCHOOL at offset 0.
input 1: 0 string SCHOOL School data
input 2: !:mime School
Then_on_your_terminal_run: file -C -m school.mgc
Create a magic file called school.mgc
that can be used with the command file
to detect School
data files. Holberton data files always contain "SCHOOL" at offset 0.
This exercise was much different from the previous exercises. From what I understand, the magic file is used to detect patterns in files and will give a specified output depending on a matching pattern.
The first argument is a number representing the offset.
The second argument is the data type you are searching for. In our case, it is a string.
The third argument is the data you are searching for. In our case, "SCHOOL", which we specified as a string in the second argument.
The fourth argument is the message you want to output on match. If the search matches, it will output this message.
The last argument is separated by a line. Since the fourth argument can be long and contain multiple strings, we separate the fourth and fifth arguments with this new line. This last argument can be multiple different things. In this case, a MIME type.
According to bash manual, a "MIME type is given on a separate line, which must be the next non-blank or comment line after the magic line that identifies the file type".
I knew to search for a MIME type because the example provided:
$ file --mime-type -m school.mgc *
The above returns message "School" when matching a MIME ?? Not exactly sure, but this is what I can tell from what I've tested out and can see from the output and examples.
$ file -m school.mgc *
The above will return message "School data" for any offset 0 "SCHOOL" matches.
A cool thing to note is that the file
command is compiling and running the magic file. So there is no need to compile to magic "permanently".
NOTE: Compiling a magic source file:
$ file -C -m .mgc
This produces the compiled magic file.
$ file -i -m .mgc *
This allows you to use the compiled file by specifying its name using the -m switch again.