Difference between revisions of "Training/Linux - command line"

From HPC
Jump to: navigation , search
m (Pysdlb moved page Linux - command line to Training/Linux - command line without leaving a redirect)
m
 
(22 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
== Introduction ==
 
== Introduction ==
  
This represents a series of teaching pages to allow you to learn more about Viper's Linux command line interface
+
This represents a series of teaching pages to allow you to learn more about Viper's Linux command-line interface
  
 
== Command Line ==
 
== Command Line ==
Line 7: Line 7:
 
Like other operating systems Linux does have a window’s type environment too called X and hopefully in the future its successor Wayland. (Linux refers to X and Wayland with the term Display Server). However, with VIPER the vast majority of work will be carried out on the command line.
 
Like other operating systems Linux does have a window’s type environment too called X and hopefully in the future its successor Wayland. (Linux refers to X and Wayland with the term Display Server). However, with VIPER the vast majority of work will be carried out on the command line.
  
The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style there are a few different ones to choose from, however the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
+
The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style, there are a few different ones to choose from. However, the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.
 +
 
 +
Below is an example of Viper's command-line interface using the terminal program [http://mobaxterm.mobatek.net/ MobaXterm].
 +
 
 +
[[File:commandline.jpg]]
  
 
== Man pages ==
 
== Man pages ==
Line 16: Line 20:
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
  
username@viper:~$ man whois' (shows manual page for the command whois)
+
username@viper:~$ man whois  (shows manual page for the command whois)
username@viper:~$ man syslog.config' (shows the manual page for a configuration file)
+
username@viper:~$ man syslog.config  (shows the manual page for a configuration file)
 
username@viper:~$ man syslogd (show the manual for a daemon (background program))
 
username@viper:~$ man syslogd (show the manual for a daemon (background program))
 
username@viper:~$ man –k syslog  (an apropos which shows a list of available man pages with this string contained within it)
 
username@viper:~$ man –k syslog  (an apropos which shows a list of available man pages with this string contained within it)
Line 80: Line 84:
 
</pre>
 
</pre>
  
There is also the character . which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.
+
There is also the character. which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.
  
 
=== Absolute and relative paths ===
 
=== Absolute and relative paths ===
 +
{|
 +
|style="width:5%; border-width: 0" | [[File:icon_pencil.png]]
 +
|style="width:95%; border-width: 0" | Although an important concept to understand the rule here is very simple. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point.
  
Although an important concept to understand the rule here is very simple. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point.
+
Here are two examples, firstly of the absolute path:
 
+
|-
Here are two examples, firstly of absolute path
+
|}
 
 
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
  
Line 165: Line 171:
 
=== rmdir ===
 
=== rmdir ===
  
This command removes the specified directory, note the directory must by empty and must not be the directory you are currently in:
+
This command removes the specified directory, note the directory must be empty and must not be the directory you are currently in:
  
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
Line 173: Line 179:
 
</pre>
 
</pre>
  
 +
== Working with files ==
 +
 +
In this section we learn how to recognise, create, remove, copy and move files using commands like '''file''', '''touch''', '''rm''', '''cp''', '''mv''' and '''rename'''.
 +
 +
Just before we start looking at these commands we need to cover two important areas:
 +
 +
* Files on Linux are case sensitive. This means that FILE1 is different from file1, and /etc/hosts are different from /etc/Hosts (the latter one does not exist on a typical Linux computer).
 +
* Everything on Linux is a file. A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1), and any process are all represented somewhere in the file system as a file.
 +
 +
=== file ===
 +
 +
This command determines the file type. Unlike Windows, Linux does not determine the file type from the extension but from examining the file header/contents itself.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ file mypicture.png
 +
pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced
 +
 +
username@viper:~$ file parallel.c
 +
parallel.c: ASCII C program text
 +
 +
</pre>
 +
 +
=== touch ===
 +
 +
This creates an empty file, which can be useful for various uses.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ touch newfile.c
 +
username@viper:~$ ls
 +
newfile.c
 +
 +
</pre>
 +
 +
 +
=== rm ===
 +
 +
Remove a file, as always be very careful with this command and without a backup, this file will be lost forever.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ ls
 +
newfile.c backup.c
 +
username@viper:~$ rm newfile.c
 +
username@viper:~$ ls
 +
backup.c
 +
 +
</pre>
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ rm –i backup.c  (this is interactive delete and will ask you to delete the file before it occurs)
 +
username@viper:~$ rm –r mydirectory  (works recursively down the specified directory but not removing any non-empty directories)
 +
username@viper:~$ rm –rf mydirectory  (works recursively down the specified directory however the –f option means force. This option is very powerful but also needs to be used with extreme care!)
 +
 +
</pre>
 +
 +
 +
As with many Linux commands there are a few options with can be used with '''rm''' (these can be view by typing '''man rm''').
 +
 +
=== cp ===
 +
 +
Copy files or directories from a source to a destination:
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ cp parallel.c mybackup.c  (copies parallel.c to mybackup.c)
 +
username@viper:~$ cp parallel.c mydirectory1  (copies parallel.c to mydirectory1)
 +
username@viper:~$ cp *c backupdirectory/  (copies all *.c files to backupdirectory)
 +
username@viper:~$ cp –r mydirectory1 mydirectory2  (copies one directory to another, note the option –r for recursive copying)
 +
 +
</pre>
 +
 +
As with many Linux commands there are a few options with can be used with '''cp''' (these can be view by typing '''man cp''').
 +
 +
=== mv ===
 +
 +
Move files from a source to a destination. A versatile command that can rename a file too:
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ mv file1.c testfile.c  (rename file1.c to testfile.c)
 +
username@viper:~$ mv directory1 directory2  (rename directory)
 +
username@viper:~$ mv file1.c /home/user/myrepo  (mv file1.c to /home/user/myrepo/file1.c)
 +
 +
</pre>
 +
 +
 +
=== rename ===
 +
 +
Although preferably to use the '''mv''' command, this command does exist to rename files
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ touch file1.c file2.c file3.c
 +
username@viper:~$ ls
 +
file1.c file2.c file3.c
 +
 +
username@viper:~$ rename .c .backup *.c
 +
username@viper:~$ ls
 +
file1.backup file2.backup file3.backup
 +
 +
</pre>
 +
 +
== Working with file contents ==
 +
 +
This section will look at working with file contents themselves, such commands are '''head''', '''tail''', '''cat''', '''tac''', '''more''' and '''less'''.
 +
 +
=== head ===
 +
 +
By default, the head command will show the first ten lines of a file.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ head /etc/passwd
 +
 +
root:x:0:0:root:/root:/bin/bash
 +
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
 +
bin:x:2:2:bin:/bin:/bin/sh
 +
sys:x:3:3:sys:/dev:/bin/sh
 +
sync:x:4:65534:sync:/bin:/bin/sync
 +
games:x:5:60:games:/usr/games:/bin/sh
 +
man:x:6:12:man:/var/cache/man:/bin/sh
 +
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
 +
mail:x:8:8:mail:/var/mail:/bin/sh
 +
news:x:9:9:news:/var/spool/news:/bin/sh
 +
 +
</pre>
 +
 +
=== tail ===
 +
 +
Similar to '''head''' but this time it will show the last 10 lines of the file by default.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ tail /etc/services
 +
vboxd 20012/udp
 +
binkp 24554/tcp # binkp fidonet protocol
 +
asp 27374/tcp # Address Search Protocol
 +
asp 27374/udp
 +
csync2 30865/tcp # cluster synchronization tool
 +
dircproxy 57000/tcp # Detachable IRC Proxy
 +
tfido 60177/tcp # fidonet EMSI over telnet
 +
fido 60179/tcp # fidonet EMSI over TCP
 +
 +
</pre>
 +
 +
=== cat ===
 +
 +
The '''cat''' command (short for concatenate) one of the most universal tools, yet all it does is copy standard input to standard output. In combination with the shell, this can be very powerful and diverse. Some examples will give a glimpse into the possibilities.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ cat /etc/resolv.conf
 +
 +
domain example.com
 +
search example.com
 +
nameserver 192.168.1.42
 +
 +
username@viper:~$ cat file1.c file2.c >file3.all  (concatenate c files into file3.all)
 +
 +
</pre>
 +
 +
=== tac ===
  
 +
Works the same as '''cat''' but will show you the file backwards:
  
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
  
 +
username@viper:~$ cat numbers
 +
one
 +
two
 +
three
  
 +
username@viper:~$ tac numbers
 +
three
 +
two
 +
one
  
 +
</pre>
  
 +
=== more ===
  
 +
The more command is useful for displaying files that take up more than one screen. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit. Some people prefer the less command to more.
 +
 +
=== less ===
 +
 +
Very similar to more but with some additional features
 +
 +
== Basic Linux Tools ==
 +
 +
This chapter introduces commands to find or locate files and to compress files, together with other common tools that were not discussed before.
 +
 +
=== find ===
 +
 +
This command is very useful to find files, more options are provided on the command line by typing '''man find'''. Here are some useful examples below:
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ find /etc  (find all files in the /etc directory)
 +
username@viper:~$ find . –name “*.conf”  (find all files that end in .conf from the current directory)
 +
username@viper:~$ find . –newer file1.c  (find all files newer than file1.c)
 +
username@viper:~$ find /etc >etcfiles.txt  (find all files but this time put them in (pipe) to the file etcfiles.txt)
 +
 +
</pre>
 +
 +
=== locate ===
 +
 +
The '''locate''' command is very different from find in that it uses an index to locate files. This is a lot faster than traversing all the directories, but it also means that it may be outdated too.
 +
 +
=== date ===
 +
 +
The '''date''' command can display the date, time, time zone, and more.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ date
 +
Sat Feb 23 12:44:30 BST 2017
 +
 +
</pre>
 +
 +
=== cal ===
 +
 +
The '''cal''' command displays the current month, with the current day highlighted.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ cal
 +
    April 2016
 +
Su Mo Tu We Th Fr Sa
 +
                1  2
 +
3  4  5  6  7  8  9
 +
10 11 12 13 14 15 16
 +
17 18 19 20 21 22 23
 +
24 25 26 27 28 29 30
 +
 +
</pre>
 +
 +
 +
=== sleep ===
 +
 +
The '''sleep''' command is sometimes used in scripts to wait a number of seconds. This example shows a five-second sleep.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ sleep 5  (five seconds later)
 +
username@viper:~$
 +
 +
</pre>
 +
 +
=== sort ===
 +
 +
The command '''sort''' will sort lines of text files. By default, the output is to the screen but this can be piped to a file or another program.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ sort myfile.txt
 +
apple
 +
banana
 +
cherry
 +
 +
</pre>
 +
 +
=== time ===
 +
 +
The time command can display how long it takes to execute a command. The date command takes only a little time.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ time date
 +
Sat Feb 23 13:08:27 BST 2016
 +
real 0m0.014s
 +
user 0m0.008s
 +
sys 0m0.006s
 +
 +
</pre>
 +
 +
=== gzip – gunzip ===
 +
 +
This is a useful compression program (like '''zip''' which also exists in Linux). The '''gzip''' command can make files take up less space.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ gzip myfile.c  (will create myfile.c.gz)
 +
username@viper:~$ gunzip myfile.c.gz  (will create myfile.c again)
 +
 +
</pre>
 +
 +
=== bzip2 – bunzip2 ===
 +
 +
Files can also be compressed with '''bzip2''' which takes a little more time than '''gzip''', but compresses better.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ bzip2 myfile.c  (will create myfile.c.bz2)
 +
username@viper:~$ bunzip2 myfile.c.bz2  (will create myfile.c again)
 +
 +
</pre>
 +
 +
=== zip – unzip ===
 +
 +
A compression program which is compatible with other '''zip''' programs found in MS Windows and other OSes.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ zip myfile.c  (will create myfile.c.zip)
 +
username@viper:~$ unzip myfile.c.zip  (will create myfile.c again)
 +
 +
</pre>
 +
 +
=== grep ===
 +
 +
The '''grep''' filter is famous among Linux (and UNIX) users. The most common use of '''grep''' is to filter lines of text containing (or not containing) a certain string.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ grep “Darren” /etc/passwd
 +
user:x:1000:1000:Darren Bird,,,:/home/user:/bin/bash
 +
 +
</pre>
 +
 +
As with most Linux commands, there are also a large number of useful options that will go with each command and '''grep''' is certainly no exception here
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ grep –i “Darren” /etc/passwd  (search in a case insensitive way)
 +
username@viper:~$ grep –r “Darren” /etc/passwd  (search recursively down any directories too)
 +
username@viper:~$ grep –v “Darren” /etc/passwd  (search for everything not containing “Darren”)
 +
 +
</pre>
 +
 +
=== wc ===
 +
 +
Counting words, lines, and characters are easy with '''wc'''.
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ wc myfile.c  (show number of words, lines, and characters)
 +
5 10 100 tennis.txt
 +
 +
</pre>
 +
 +
== File Permissions ==
 +
 +
=== Introduction ===
 +
 +
Similar to many other operating systems Linux uses a method of access rights on files and directories. These can be view by using the ls command
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ ls –l  (option l is for long listing)
 +
 +
-rw-r--r--  1 dbird admin    12211 May 11  2016 template.countries
 +
-rw-r--r--  1 dbird admin    3097 May 11  2016 template.languages    (1)
 +
drwxr-x---  2 dbird admin    4096 Dec  1 10:54 Templates      (2)
 +
-rw-r--r--  1 dbird admin    8087 Feb 12 14:05 temp.txt
 +
-rwx------  1 dbird admin    1110 Mar  8 10:53 test4pisignage.pl    (3)
 +
 +
</pre>
 +
 +
Each file and directory has access rights that are associated with each one. When we look at the 10 symbol string above on the left-hand side (e.g. '''drwxr-xr-x''').
 +
 +
* The first letter present whether the file is a directory or not.
 +
* The next three represent the file permission for the user that owns that file (i.e. dbird in this example).
 +
* The next three represent the file permission of the group to whom that user belongs (i.e. group admin).
 +
* The last three represent the file permissions for everyone else (i.e. all users).
 +
 +
For each of the permission parts the letters mean the following in their groups:
 +
 +
* r indicates read permission to read and copy the file, its absence indicates this is not available.
 +
* w indicates write permission to write the file, its absence indicates this is not available.
 +
* x indicates execution permission to allow the file to be executed, its absence indicates this is not available.
 +
 +
 +
Using the example above would mean:
 +
 +
* Example '''(1)''' has read/write access for user dbird and read access only for everyone else.
 +
* Example '''(2)''' is a directory with full access for user dbird and read access for only users in the admin group.
 +
* Example '''(3)''' is an application which is only accessible by the user dbird, note not only is it read and write but it also has its ‘execution’ permission set for that user also.
 +
 +
 +
=== Changing access rights ===
 +
 +
This command allows the user to change file (and directory) permissions.
 +
 +
* u User
 +
* g Group
 +
* o Other
 +
* a All
 +
* r Read
 +
* w Write (and erase)
 +
* x Execution (and access directory
 +
* + Add permission
 +
* - Remove permission
 +
 +
 +
<pre style="background-color: black; color: white; border: 2px solid black; font-family: monospace, sans-serif;">
 +
 +
username@viper:~$ chmod go-rwx myfile.c  (remove read, write and execute permissions removed for group and other)
 +
username@viper:~$ chmod u+x myapp.pl  (make the program myapp.pl executable to the user (i.e. the owner of the file))
 +
 +
</pre>
  
  
  
 
== Further Information ==
 
== Further Information ==
 +
 +
* [https://uk.mathworks.com/products/parallel-computing.html?s_tid=srchtitle Mathworks parallel computing toolbox home page]
 +
 +
 +
==Navigation==
 +
 +
* [[Main_Page|Home]]
 +
* [[Applications|Application support]]
 +
* [[General|General]] *
 +
* [[Programming|Programming support]]

Latest revision as of 12:58, 24 May 2021

Introduction

This represents a series of teaching pages to allow you to learn more about Viper's Linux command-line interface

Command Line

Like other operating systems Linux does have a window’s type environment too called X and hopefully in the future its successor Wayland. (Linux refers to X and Wayland with the term Display Server). However, with VIPER the vast majority of work will be carried out on the command line.

The command line in Linux is referred to as a shell. The shell is a program that allows the user to interact with Linux at the command line. In true Linux style, there are a few different ones to choose from. However, the one used predominantly is BASH. The name BASH is an acronym for “Bourne Again SHell”, a reference to BASH is an enhanced replacement for sh, the original Unix shell program written by Steve Bourne.

Below is an example of Viper's command-line interface using the terminal program MobaXterm.

Commandline.jpg

Man pages

This will explain the use of man pages (also called manual pages) on Linux. Most Linux files and commands have pretty good man pages to explain their use. Type man followed by a command (for which you want help) and start reading. Press q to quit the man page. See below:


username@viper:~$ man whois  (shows manual page for the command whois)
username@viper:~$ man syslog.config  (shows the manual page for a configuration file)
username@viper:~$ man syslogd (show the manual for a daemon (background program))
username@viper:~$ man –k syslog  (an apropos which shows a list of available man pages with this string contained within it)

Working with directories

As with other operating systems such as Microsoft Windows the filesystem is based around files and directories. Linux is no exception to this and uses a number of commands for the user to navigate around its own filesystem.

This module is a brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux system.

This section will also discuss absolute and relative paths and path completion in the bash shell.

pwd

On the command line pwd (or print working directory) basically displays the current directory you are in. This would appear as:


username@viper:~$ pwd
/home/dbird

cd

On the command line cd (or change directory) changes your current directory to the one specified:


username@viper:~$ cd /var
username@viper:~$ pwd
/var
username@viper:~$ cd /home/user
username@viper:~$ pwd
/home/user

There is also a shortcut back to your home directory by typing the character ~ (tilda) which has the same effect as typing (for example) /home/user.


username@viper:~$ cd ~
username@viper:~$ pwd
/home/user

To go to the directory above (or parent directory), we use the characters ..


username@viper:~$ pwd
/home/user

username@viper:~$ cd ..
username@viper:~$ pwd
/home

There is also the character. which means the current directory. This is not useful for the cd command but is very useful for copying files to your current directory.

Absolute and relative paths

Icon pencil.png Although an important concept to understand the rule here is very simple. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you don't start your path with a slash, then the current directory is the assumed starting point.

Here are two examples, firstly of the absolute path:


username@viper:~$ pwd
/home/user

user@viper:~$ cd /var
user@viper:~$ pwd
/var

And relative path


username@viper:~$ pwd
/home/user

username@viper:~$ cd myfiles
username@viper:~$ pwd
/home/user/myfiles


The command line will help you in typing a path without errors. If you type a partial command such as:


username@viper:~$ cd /home/user/my

Pressing the TAB key will fill in the rest of the directory name, if that directory exists and it is unique). So the following would appear


username@viper:~$ cd /home/user/myfiles

ls

This command lists the contents of a directory:


username@viper:~$ ls
myfile1.txt myfile2.txt mydirectory1

ls has a number of useful different options


username@viper:~$ ls –l  (show a long listing with more information)
username@viper:~$ ls –a  (show all files including those that are hidden)
username@viper:~$ ls –la  (combines both of the options above)

mkdir

This commands makes a directory from the specified directory:


username@viper:~$ mkdir mydirectory1
username@viper:~$ cd mydirectory1
username@viper:~$ pwd
/home/user/mydirectory1

rmdir

This command removes the specified directory, note the directory must be empty and must not be the directory you are currently in:


username@viper:~$ rmdir mydirectory1

Working with files

In this section we learn how to recognise, create, remove, copy and move files using commands like file, touch, rm, cp, mv and rename.

Just before we start looking at these commands we need to cover two important areas:

  • Files on Linux are case sensitive. This means that FILE1 is different from file1, and /etc/hosts are different from /etc/Hosts (the latter one does not exist on a typical Linux computer).
  • Everything on Linux is a file. A directory is a special kind of file, but it is still a (case sensitive!) file. Each terminal window (for example /dev/pts/4), any hard disk or partition (for example /dev/sdb1), and any process are all represented somewhere in the file system as a file.

file

This command determines the file type. Unlike Windows, Linux does not determine the file type from the extension but from examining the file header/contents itself.


username@viper:~$ file mypicture.png
pic33.png: PNG image data, 3840 x 1200, 8-bit/color RGBA, non-interlaced

username@viper:~$ file parallel.c
parallel.c: ASCII C program text

touch

This creates an empty file, which can be useful for various uses.


username@viper:~$ touch newfile.c
username@viper:~$ ls
newfile.c


rm

Remove a file, as always be very careful with this command and without a backup, this file will be lost forever.


username@viper:~$ ls
newfile.c backup.c
username@viper:~$ rm newfile.c
username@viper:~$ ls
backup.c


username@viper:~$ rm –i backup.c   (this is interactive delete and will ask you to delete the file before it occurs)
username@viper:~$ rm –r mydirectory   (works recursively down the specified directory but not removing any non-empty directories)
username@viper:~$ rm –rf mydirectory   (works recursively down the specified directory however the –f option means force. This option is very powerful but also needs to be used with extreme care!)


As with many Linux commands there are a few options with can be used with rm (these can be view by typing man rm).

cp

Copy files or directories from a source to a destination:


username@viper:~$ cp parallel.c mybackup.c  (copies parallel.c to mybackup.c)
username@viper:~$ cp parallel.c mydirectory1   (copies parallel.c to mydirectory1)
username@viper:~$ cp *c backupdirectory/  (copies all *.c files to backupdirectory)
username@viper:~$ cp –r mydirectory1 mydirectory2  (copies one directory to another, note the option –r for recursive copying)

As with many Linux commands there are a few options with can be used with cp (these can be view by typing man cp).

mv

Move files from a source to a destination. A versatile command that can rename a file too:


username@viper:~$ mv file1.c testfile.c  (rename file1.c to testfile.c)
username@viper:~$ mv directory1 directory2  (rename directory)
username@viper:~$ mv file1.c /home/user/myrepo  (mv file1.c to /home/user/myrepo/file1.c)


rename

Although preferably to use the mv command, this command does exist to rename files


username@viper:~$ touch file1.c file2.c file3.c
username@viper:~$ ls
file1.c file2.c file3.c

username@viper:~$ rename .c .backup *.c
username@viper:~$ ls
file1.backup file2.backup file3.backup

Working with file contents

This section will look at working with file contents themselves, such commands are head, tail, cat, tac, more and less.

head

By default, the head command will show the first ten lines of a file.


username@viper:~$ head /etc/passwd

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
lp:x:7:7:lp:/var/spool/lpd:/bin/sh
mail:x:8:8:mail:/var/mail:/bin/sh
news:x:9:9:news:/var/spool/news:/bin/sh

tail

Similar to head but this time it will show the last 10 lines of the file by default.


username@viper:~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp 		# binkp fidonet protocol
asp 27374/tcp 		# Address Search Protocol
asp 27374/udp
csync2 30865/tcp 		# cluster synchronization tool
dircproxy 57000/tcp 	# Detachable IRC Proxy
tfido 60177/tcp 		# fidonet EMSI over telnet
fido 60179/tcp 		# fidonet EMSI over TCP

cat

The cat command (short for concatenate) one of the most universal tools, yet all it does is copy standard input to standard output. In combination with the shell, this can be very powerful and diverse. Some examples will give a glimpse into the possibilities.


username@viper:~$ cat /etc/resolv.conf

domain example.com
search example.com
nameserver 192.168.1.42

username@viper:~$ cat file1.c file2.c >file3.all  (concatenate c files into file3.all)

tac

Works the same as cat but will show you the file backwards:


username@viper:~$ cat numbers
one
two
three

username@viper:~$ tac numbers
three
two
one

more

The more command is useful for displaying files that take up more than one screen. More will allow you to see the contents of the file page by page. Use the space bar to see the next page, or q to quit. Some people prefer the less command to more.

less

Very similar to more but with some additional features

Basic Linux Tools

This chapter introduces commands to find or locate files and to compress files, together with other common tools that were not discussed before.

find

This command is very useful to find files, more options are provided on the command line by typing man find. Here are some useful examples below:


username@viper:~$ find /etc   (find all files in the /etc directory)
username@viper:~$ find . –name “*.conf”   (find all files that end in .conf from the current directory)
username@viper:~$ find . –newer file1.c   (find all files newer than file1.c)
username@viper:~$ find /etc >etcfiles.txt   (find all files but this time put them in (pipe) to the file etcfiles.txt)

locate

The locate command is very different from find in that it uses an index to locate files. This is a lot faster than traversing all the directories, but it also means that it may be outdated too.

date

The date command can display the date, time, time zone, and more.


username@viper:~$ date
Sat Feb 23 12:44:30 BST 2017

cal

The cal command displays the current month, with the current day highlighted.


username@viper:~$ cal
     April 2016
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


sleep

The sleep command is sometimes used in scripts to wait a number of seconds. This example shows a five-second sleep.


username@viper:~$ sleep 5  (five seconds later)
username@viper:~$

sort

The command sort will sort lines of text files. By default, the output is to the screen but this can be piped to a file or another program.


username@viper:~$ sort myfile.txt
apple
banana
cherry

time

The time command can display how long it takes to execute a command. The date command takes only a little time.


username@viper:~$ time date
Sat Feb 23 13:08:27 BST 2016
real 0m0.014s
user 0m0.008s
sys 0m0.006s

gzip – gunzip

This is a useful compression program (like zip which also exists in Linux). The gzip command can make files take up less space.


username@viper:~$ gzip myfile.c   (will create myfile.c.gz)
username@viper:~$ gunzip myfile.c.gz   (will create myfile.c again)

bzip2 – bunzip2

Files can also be compressed with bzip2 which takes a little more time than gzip, but compresses better.


username@viper:~$ bzip2 myfile.c  (will create myfile.c.bz2)
username@viper:~$ bunzip2 myfile.c.bz2  (will create myfile.c again)

zip – unzip

A compression program which is compatible with other zip programs found in MS Windows and other OSes.


username@viper:~$ zip myfile.c   (will create myfile.c.zip)
username@viper:~$ unzip myfile.c.zip   (will create myfile.c again)

grep

The grep filter is famous among Linux (and UNIX) users. The most common use of grep is to filter lines of text containing (or not containing) a certain string.


username@viper:~$ grep “Darren” /etc/passwd 
user:x:1000:1000:Darren Bird,,,:/home/user:/bin/bash

As with most Linux commands, there are also a large number of useful options that will go with each command and grep is certainly no exception here


username@viper:~$ grep –i “Darren” /etc/passwd  (search in a case insensitive way)
username@viper:~$ grep –r “Darren” /etc/passwd  (search recursively down any directories too)
username@viper:~$ grep –v “Darren” /etc/passwd  (search for everything not containing “Darren”)

wc

Counting words, lines, and characters are easy with wc.


username@viper:~$ wc myfile.c  (show number of words, lines, and characters)
5 10 100 tennis.txt

File Permissions

Introduction

Similar to many other operating systems Linux uses a method of access rights on files and directories. These can be view by using the ls command


username@viper:~$ ls –l   (option l is for long listing)

-rw-r--r--  1 dbird admin    12211 May 11  2016 template.countries
-rw-r--r--  1 dbird admin     3097 May 11  2016 template.languages    (1)
drwxr-x---  2 dbird admin     4096 Dec  1 10:54 Templates       (2)
-rw-r--r--  1 dbird admin     8087 Feb 12 14:05 temp.txt
-rwx------  1 dbird admin     1110 Mar  8 10:53 test4pisignage.pl    (3)

Each file and directory has access rights that are associated with each one. When we look at the 10 symbol string above on the left-hand side (e.g. drwxr-xr-x).

  • The first letter present whether the file is a directory or not.
  • The next three represent the file permission for the user that owns that file (i.e. dbird in this example).
  • The next three represent the file permission of the group to whom that user belongs (i.e. group admin).
  • The last three represent the file permissions for everyone else (i.e. all users).

For each of the permission parts the letters mean the following in their groups:

  • r indicates read permission to read and copy the file, its absence indicates this is not available.
  • w indicates write permission to write the file, its absence indicates this is not available.
  • x indicates execution permission to allow the file to be executed, its absence indicates this is not available.


Using the example above would mean:

  • Example (1) has read/write access for user dbird and read access only for everyone else.
  • Example (2) is a directory with full access for user dbird and read access for only users in the admin group.
  • Example (3) is an application which is only accessible by the user dbird, note not only is it read and write but it also has its ‘execution’ permission set for that user also.


Changing access rights

This command allows the user to change file (and directory) permissions.

  • u User
  • g Group
  • o Other
  • a All
  • r Read
  • w Write (and erase)
  • x Execution (and access directory
  • + Add permission
  • - Remove permission



username@viper:~$ chmod go-rwx myfile.c   (remove read, write and execute permissions removed for group and other)
username@viper:~$ chmod u+x myapp.pl   (make the program myapp.pl executable to the user (i.e. the owner of the file))


Further Information


Navigation