Computer Systems with Project Operating 2023 (2024)

Gnome

When you log in to the department Linux system using the ThinLinc client you will be usingthe Gnome graphical desktop environment.

The Gnome terminal

You can open a terminal in two ways:

  • From the Applicatinos menu at the top left of the desktop: ApplicationsAccessoriesTerminal.
  • By pressing the keyboard shorcut CTRL + ALT + T .

After a few seconds a new terminal window should open.

Computer Systems with Project Operating 2023 (1)

In the upper left corner of the white area of the terminal window you seeabcd1234@arrhenius:~$. This it the shell prompt with your username on the formabcd1234 and the name of the Linux server you areconnected to, in this example arrhenius. The shell prompt you see might bedifferent.

The shell prompt

In the above example, the prompt shows the username of the logged in user abcd1234together with the name of the physical Linux server arrhenius used. You shouldsee your own user name. If you are logged into a different physical Linux serveryou will also see a different server name in the prompt.

It is also possible to tweak the prompt to show custominformation such as your username, local time etc.

Since the appearance of the shell prompt might vary, in all further instructions $ will be used to refer tothe Linux shell prompt. In the instructions, interaction withthe Linux shell in the terminal will be presented in a box like this.

$

Your username (whoami)

Every user on the Linux system has a unique username. The whoami command will show your username.Type whoami at the shell prompt.

$ whoami

Press enter to execute the command. Now the result will be printed on the nextline in the terminal and a new shell prompt will appear on the line after that.

$ whoamiabcd1234$

In the above example the username of the logged in user abcd234 is printed asthe result of the whoami command.

Username

In all examples and instructions you should replace abcd1234 with your actual username.

Print working directory (pwd)

The shell has a concept of a current working directory. The pwd (print workingdirectory) commands prints the full path of the current working directory.

Type pwd at the shell prompt.

$ pwd

Press enter to execute the command.

$ pwd/home/abcd1234$

In the above example the current working directory /home/abcd1234 is printedas the result of the pwd command.

Home directory

On the Linux system each user has a private home directory to where she/hecan save files and create sub directories.

When you first log in to the Linux system the home directory will be used as thecurrent working directory in the shell.

For user abcd1234 the full path to the home directory is /home/abcd1234.

List files and directories (ls)

To list the files and directories in the current working directory the ls command can be used. The name ls is a short form of list (files).

Type ls at the shell prompt.

$ ls

Press enter to execute the ls command. You should see something similar to thebelow as result but you might see other files and folders listed.

$ lsfoo.txt Desktop public_html$ 

In the above example the only content in the current working directory is thetext file foo.txt and two sub directories Desktop and public_html. You may seemany more directories and files.

Distinguish between files and folders (ls -F)

To get some more information about files and folder various options can be givento the ls command. One useful option is -F that marks directorieswith a trailing slash /.

$ ls -Ffoo.txt Desktop/ public_html/$ 

Visualize a directory as a tree

The tree command displays the contents of the current directory andsubdirectories as a tree structure.

The output takes a graphical form which will resemble the following example:

.├── README.md├── one.txt├── sub│ └── three.txt└── two.txt1 directory, 4 files

In the above example, there are three files (README.md, one.txt andtwo.txt) and one sub directory (sub) in the current working directory. Inthe sub directory sub there is a single file three.txt.

You can provide three with the path to a directory to visualize its content.

$ tree subsub/└── three.txt0 directories, 1 file

Change directory (cd)

The cd command navigates to a different folder. The name cd means change directory.

First print the current working directory.

$ pwd/home/abcd1234$

To navigate to the Desktop folder, type cd Desktop at the shell prompt and pressenter.

$ pwd/home/abcd1234$ cd Desktop$

Now, execute the pwd command.

$ pwd/home/abcd1234$ cd Desktop$ pwd/home/abcd1234/Desktop$

Note how the current working directory changed from /home/abcd1234 to/home/abcd1234/Desktop as the result of the cd Desktop command.

The directory above the current working directory can be referred to using ... To navigate to the parent directory, type cd .. and press enter.

$ pwd/home/abcd1234$ cd Desktop$ pwd/home/abcd1234/Desktop$ cd ..$

Now, execute the pwd command again.

$ pwd$ /home/abcd1234$ cd Desktop$ pwd/home/abcd1234/Desktop$ cd ..$ pwd/home/abcd1234$

Note how the current working directory changed back from /home/abcd1234/Desktopto /home/abcd1234 as the result of the cd .. command.

Print content of file to the terminal (cat)

The cat command can be used to print the content of a file to the terminal.

Assume you have the following file named foo.txt in the current working directory.

The first line of the file.The third line. The second line is empty.The last line of the file.

You can now print the content of foo.txt to the terminal using the cat command.

$ cat foo.txtThe first line of the file.The third line. The second line is empty.The last line of the file.$

The name cat is a short formof concatenate which means tojoin together. If more than one argument is given to cat the contents of theprovided files will be joined together and printed to the terminal.

In the below example cat is used to concatenate the file foo.txt with itself.

$ cat foo.txt foo.txtThe first line of the file.The third line. The second line is empty.The last line of the file.The first line of the file.The third line. The second line is empty.The last line of the file.$

One useful option to the cat command is -n which prefixes each line with a line number.

$ cat -n foo.txt 1The first line of the file. 2 3The third line. The second line is empty. 4The last line of the file.$

Count words, lines and bytes (wc)

The wc command counts the number of words, lines and bytes.

$ wc foo.txt 4 20 98$

In the above example we see that the file foo.txt contains for lines, 20 words and 98 bytes.

Filter (grep)

The grep command searches its input for a pattern and prints all lines in theinput that contains that pattern.

To search for the the string X in the input type grep X at the shell promptand press enter.

$ grep X

Note that we don’t get back the shell prompt. This is because the grep command is still running waiting for input.The grep command will now read input from the terminal and print back alllines containing the character X.

Now type Hello and press enter.

$ grep XHello

There is no X in the string Hello and therefore grep will not print back the string Hello to the terminal.

Type Hello mr X and press enter and watch what happens.

$ grep XHelloHello mr XHello mr X

Once you type Hello mr X the grep command will print Hello mr X right back tothe terminal since it contains a matching X.

Lets try a few more lines and observe what happens.

$ grep XHelloHello mr XHello mr XabcabcXdefabcXdefxxx

Only lines containing a matching X will be echoed back to the terminal.

No more input

To tell grep that you are done (no more input), press Ctrl D (press and holddown the control key and while you still hold down the control key press theD key).

Press Ctrl D. Now grep terminates and you get back to the shell prompt.

$ grep XHelloHello mr XHello mr XabcabcXdefabcXdefABC$

To filter the lines i a file, the name of the file can be given together with a search pattern to grep.

Assume you have the file foo.txt in your current directory.

$ cat foo.txtThe first line of the file.The third line. The second line is empty.The last line of the file.$

In the below example only lines containing of in the file foo.txt will be printed to the terminal.

$ grep of foo.txtThe first line of the file.The last line of the file.$

Filter the output of ls using grep (ls | grep)

The usefulness of grep might not obvious at this point. To make grep usefulwe will combine grep with ls to filter the output of ls.

First we use ls to list all files and folders.

$ lsfoo.txt Desktop public_html$ 

If we are only interested in files (and folders) with names ending in .txt wecan combine ls and grep to using the pipe character |.

$ ls | grep .txtfoo.txt$ 

In the above example, first the ls command exectutes but it does not print itsresult back to the terminal. Instead, the result of the ls command becomes theinput to the grep command. The only file or folder name containing .txt isfoo.txt.

Piping commands togehter

Using the pipe character | the output of the command to the left becomes theinput to the command to the right. This is called piping the two commandstogether.

Compressed file arhives (tarballs)

It is often useful to compress multiple files and folders into a single filethat can later be decompressed and expanded to get back the original files andfolders. There exists many file formats for compressed file archives.

  • Windows users commonly use the zip file format.
  • Unix users commonly use the tar file format.

Tarball

The name tarball is often used to refer to a tar archive file.

Download the following gziped compressed tar archive (tarball) to your home folder:

  • archive.tar.gz

Verify that you have the tarball in your current working directory

From the terminal, make sure you have the downloaded tarball in the currentworking directory.

$ ls | grep .tararchive.tar.gz$ 

In the above example the output of ls is piped together with grep to filter theoutput of ls to only print any files (or folders) containing .tar. Youshould see archive.tar.gz among the results.

Sneak peek inside a tarball (tar tf)

To see the contents of a tarball without extracting all the files you can usetar with options t and f.

$ tar tf archive.tar.gzarchive/archive/large.txtarchive/small.txtarchive/sub_folder/archive/sub_folder/info.txt$

In the above example we see that the tarball archive.tar.gz contains the toplevel directory archive with sub folder sub_folder. In the top level directoryarchive there are two files (large.txt and small.txt) and in the sub foldersub_folder there is a single file (small.txt).

Unpack a tarball (tar xvfz)

To unpack and extract the contents of a gzipped tarball we need to use the xvfzoptions together with the tar command.

$ tar xvfz archive.tar.gzx archive/x archive/large.txtx archive/small.txtx archive/sub_folder/x archive/sub_folder/info.txt$

Now the tarball have been unpacked. Use ls to see what happened to the currentworking directory.

$ ls | grep archivearchivearchive.tar.gz$ 

In the above example we now have a new directory named archive inside thecurrent working directory.

Use cd to “step inside” the archive directory and then ls -F to list thecontent.

$ cd archive$ ls -Flarge.txtsmall.txtsub_folder/$ 

Using the -R option ls will be run recursively stepping inside every sub-directory.

$ ls -Rlarge.txtsmall.txtsub_folder./sub_folder:info.txt$

In the result printed by ls -R a single period . means the current workingdirectory.

Print text back to the terminal (echo)

To print anything to the terminal simply type echo followed by the text you wantto print.

$ echo HelloHello$

Note that HELLO is echoed back to the terminal as the result of executing theecho Hello command before the shell prints the next command prompt.

Shell variables

The shell can set and read variables. Sometimes it is useful to use the value ofa built-in shell variable to make a command more generic and/or portable.

Remember that the command woami can be used to print your username.

$ whoamiabcd1234$

$USER

An alternative to woami is to use echo together with the shell variableUSER. In order for echo to know if you want to print the string "USER" orthe value of the shell variable USER shell variables must be prefixed with $or enclosed within ${ }.

$ echo Hello USERHello USER$ echo Hello $USERHello abcd1234$ echo Hello ${USER}Hello abcd1234$

$HOME

Another useful shell variable is HOME with the full path to the home directoryfor the logged in user.

$ echo $HOME/home/abcd1234$

Command history

Often you type and run a command in the terminal and later you wants to run thevery same command again. To prevent you from having to type the same thing againthe shell keeps a history of executed command. To navigate the history, simplypress the up-arrow to move backwards in history and press the down-arrow to moveforward in history.

Try the following command in the terminal.

$ pwd/home/abcd1234$

And now this command.

$ whoamiabcd1234$

If you want to repeat the whoami command, simply press the up-arrow key once.Instead if you wish to run the pwd command again, press the up-arrow key twice.

Reading manual pages (man)

For more information about command you can always refer to the correspondingbuilt in manual page. For example, to read the manual page for the ls commandsimply type man ls and press enter at the shell prompt.

$ man ls

This will print the manual one page at a time to the terminal. To view the nextpage, press the space bar. To quit, press q.

To learn more about the build in manual pages read the manual page about the mancommand.

$ man man

A summary of usefull controll keys when reading man pages.

KeyBehaviour
qQuit and get back to the terminal
Space bar or FMove forward one page
DMove forward half a page
BMove backwards one page
UMove backwards half a page

Learn more

To learn more about the Ubuntu Linux shell:

To learn more about tar file archives (tarballs):

Computer Systems with Project Operating 2023 (2024)
Top Articles
Latest Posts
Article information

Author: Jonah Leffler

Last Updated:

Views: 5994

Rating: 4.4 / 5 (65 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Jonah Leffler

Birthday: 1997-10-27

Address: 8987 Kieth Ports, Luettgenland, CT 54657-9808

Phone: +2611128251586

Job: Mining Supervisor

Hobby: Worldbuilding, Electronics, Amateur radio, Skiing, Cycling, Jogging, Taxidermy

Introduction: My name is Jonah Leffler, I am a determined, faithful, outstanding, inexpensive, cheerful, determined, smiling person who loves writing and wants to share my knowledge and understanding with you.