Learn the fundamentals of UNIX
UNIX is a widely used operating system in the field of programming, which among many other things allows you to access and interact directly with computer resources via a terminal. This can be very useful in situations where you cannot use an interface, such as a VPS that to save memory does not have it active.
Let’s introduce the Shell before.
The Shell
The shell is the interface between the user and the operating system in the UNIX command line interface. The shell is a program that interprets commands and passes them to the operating system to be executed. There are many different shells available in the UNIX command line interface, including Bash, Zsh, and Ksh.
The shell provides a command prompt that allows the user to enter commands and interact with the operating system. The command prompt typically displays information about the current working directory, the user’s username, and the hostname of the machine. The user can then enter commands at the command prompt and the shell will execute them.
Shell Scripts
A shell script is a program written in the shell programming language that can be executed by the shell. Shell scripts are typically used to automate tasks or perform repetitive actions in the UNIX command line interface.
To create a shell script, you can use a text editor to write the commands you want to execute, and save the file with a .sh
extension.
To execute a shell script, you need to add the execution permission to it (explained later using chmod
), then you can use the ./
operator followed by the name of the script. For example, to execute a shell script called myscript.sh
, you would type ./myscript.sh
in the command line.
But now let’s look in detail at some of the ways you can interact with files by using UNIX!
List Current Directory
pwd
let you see the currently working directory.
$ pwd
/Users/user/Desktop/glizzy
List Directory Contents
The ls
command let you display the contents of a directory. By default is gonna show only the name of the files and directories inside the working directory, but by adding some other parameters such as l
you can get much more infos. Here’s an example:
$ ls -l
total 4
drwxr-xr-x 2 user user 4096 Feb 16 11:02 documents
-rw-r--r-- 1 user user 0 Feb 16 11:03 myfile.txt
Where each column gives you a different info about the file:
- Permissions the file permissions about who can read, write and execute. But how can you understand that strange line? is simple, take a look: drwxr-xr-x the first charapter tells you what type of file it is, in this case ‘d’ means directory, followed by 3 groups of 3 elements in the format
rwx
(write-execute-read). In order the permissions are for the owner, the group of users owning the file, everyone.
- Links numbers of hard links connected to a file. An hard link is something like a shortcut of that file.
- Owner: The username of the file owner.
- Group: The group name of the file owner.
- Size: The size of the file in bytes.
- Date: The date and time when the file was last modified.
- Name: The name of the file.
Change a file permission
chmod
is the command to do that. By changing the file permissions you can determinate who can read (r), write (w) and execute (x), between owner (u), group of users who own the file (g) and all the other users (o).
In this example I’m gonna set the example.sh to be accessible for writing, executing and reading from the owner, reading and executing from the group of owners, and only execute for everyone else.
chmod u=+w,+x,+r example.sh
chmod g=+r,-w,+x example.sh
chmod o=-w,-x,+x example.sh
A more advanced usage could be done by using numbers instead of letters, here’s an example
chmod 751 example.sh
751, based on the following table is gonna give the same output as before.
Change Directory
cd
is gonna do the work. For example if I’m inside the folder “Downloads” and I wanna move into “test” (a subfolder of Downloads) I can run
cd /test
Create Directory
The mkdir
command is used to create a new directory. For example, if you wanted to create a new directory called 'pictures', you would type
mkdir pictures
Remove Files and Directories
The rm
command is used to remove files from the system.
To remove an entire directory and its contents, use the -r
option. For example, to remove a file called myfile.txt
, you would type:
rm myfile.txt
To remove a directory called pictures
and its contents, you would type:
rm -r pictures
Move or Rename Files
The mv
command is used to move or rename files. For example, to move a file called myfile.txt
to a directory called pictures
, you would type:
mv myfile.txt pictures/
If you wanted to rename the file to newfile.txt
, you would type:
mv myfile.txt newfile.txt
Copy Files
The cp
command is used to copy files from one location to another. For example, to copy a file called myfile.txt
to a directory called pictures
, you would type
cp myfile.txt pictures/
Compress and Extract Files
The tar
command is used to compress and extract files. To compress a directory and its contents, use the cf
options followed by a filename. For example, to create a file called archive.tar
that contains the contents of the documents
directory, you would type:
tar cf archive.tar documents/
To extract the contents of an archive file, use the xf
options followed by the filename. For example, to extract the contents of the archive.tar
file, you would type:
tar xf archive.tar
Create Empty Files
The touch
command is used to create an empty file. For example, to create a file called newfile.txt
, you would type touch newfile.txt
. If the file already exists, the touch
command will update the modification time of the file.
In addition to creating new files, the touch
command can be used to update the modification time of an existing file. This can be useful in situations where you need to trigger a script or process when a file is modified. For example, you could use the touch
command to update the modification time of a file called data.csv
to trigger a data import script:
$ touch data.csv
In addition, the touch
command can be used to set a specific modification or access time for a file using the -t
option. The timestamp must be in the format [[CC]YY]MMDDhhmm[.SS]
, where CC
and YY
are the century and year, MM
is the month, DD
is the day, hh
is the hour, mm
is the minute, and SS
is the second. For example, to set the modification time of a file called myfile.txt
to January 1, 2023 at 12:00pm, you would type:
touch -t 202301011200 myfile.txt
Display File Contents
The cat
command is used to display the contents of a file. For example, to display the contents of a file called myfile.txt
, you would type cat myfile.txt
in the command line. The output of the cat
command is the contents of the file as plain text.
In addition to displaying the contents of a single file, the cat
command can also be used to concatenate files. For example, to concatenate the contents of two files called file1.txt
and file2.txt
, you would type cat file1.txt file2.txt
. The output of the command is the contents of both files combined.
The cat
command can also be used to display the contents of multiple files at once. For example, to display the contents of two files called file1.txt
and file2.txt
in the same output, you would type:
cat file1.txt file2.txt
The output of the command is the contents of both files displayed sequentially.
Another useful feature of the cat
command is its ability to redirect the output to a new file. For example, to concatenate the contents of two files called file1.txt
and file2.txt
and save the result to a new file called combined.txt
, you would type.
cat file1.txt file2.txt > combined.txt
The >
symbol is used to redirect the output of the cat
command to a new file.
Apply Changes to Files
The patch
command is used to apply changes to a file based on a patch file. A patch file contains a list of changes to a file, such as additions, deletions, or modifications. For example, to apply changes from a patch file called my_patch
to a file called myfile.txt
, you would type patch myfile.txt my_patch
in the command line.
Set and Use Variables
You can set variables in the UNIX command line interface using the =
operator. For example, to set a variable called myvar
to the value hello
, you would type myvar=hello
in the command line. You can then use the variable in other commands by enclosing it in $
symbols. For example, to display the value of the myvar
variable, you would type echo $myvar
.
Manipulate Command Outputs
You can manipulate the output of a command using pipes (|
) and other commands. For example, to display only the IP address of a network interface using the ifconfig
command, you could use the following command: ifconfig eth0 | grep "inet addr" | cut -d: -f2 | awk '{print $1}'
. This command uses the grep
command to search for the line containing the IP address, the cut
command to extract the IP address from the output, and the awk
command to print only the IP address.
You can also redirect the output of a command to a file using the >
symbol. For example, to save the output of the ifconfig
command to a file called network_config.txt
, you would type ifconfig > network_config.txt
in the command line.
- grep:
grep
is a command used to search for specific patterns in the output of a command. For example, to search for all occurrences of the word "error" in the output of a command, you would typecommand | grep error
. The output of thegrep
command is the lines that contain the word "error". - cut:
cut
is a command used to extract specific columns or fields from the output of a command. For example, to extract the first and third fields of a line of text separated by a comma, you would typecommand | cut -d ',' -f 1,3
. The-d
option specifies the delimiter, and the-f
option specifies the fields to extract. - awk:
awk
is a powerful text processing tool that can be used to extract, manipulate, and analyze text data. For example, to extract the second column of a tab-separated output, you would typecommand | awk '{print $2}'
. The$2
specifies the second column. - sed:
sed
is a command used to perform text transformations on the output of a command. For example, to replace all occurrences of the word "cat" with "dog" in the output of a command, you would typecommand | sed 's/cat/dog/g'
. Thes
command is used to substitute text, and theg
option specifies a global substitution (replace all occurrences, not just the first). - Redirection and pipes: As mentioned earlier, you can redirect the output of a command to a file using the
>
symbol. You can also use pipes (|
) to send the output of one command as input to another command. For example, to count the number of lines in the output of a command, you would typecommand | wc -l
. Thewc
command is used to count words, lines, and characters in a text file or output.
Display Manual Pages
The man
command is used to display the manual pages for a command. For example, to view the manual page for the ls
command, you would type man ls
. The manual page provides detailed information about the command's options, usage, and examples. The man
command can be used for any command in the UNIX command line interface.
This command is very useful. All of the described commands got many other parameters that you can use, so always read the documentation!
Conclusion
With this article you can get a first basis for interacting with files on a computer via UNIX, so that you can perform virtually all the actions you would do having a desktop, from a terminal.
I hope it has helped you, but don’t stop there! Read the documentations (via man) of the commands you need, look for other commands you might need, the list is endless!
If you have any questions or feedback, feel free to reach out to me via glizzykingdreko@protonmail.com.
You can also check out my Twitter and Github for more updates and projects.