20160324_unix
20160324_unix
Contents
1 Introduction 2
1.1 Unix architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
2 Before starting 3
2.1 Common vocabulary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Useful keyboard short-cuts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
7 Packages 10
8 A few links 11
1
IPSL BootCamp: 2016/03/24. Unix commands 2
1 Introduction
Unix is an operating system available on many computers, from PCs to supercomputers.1
When you log onto a Unix system, your main interface to the system is called the Unix shell that is
a command-line interface allowing you to interact with a computer program via successive lines of text
(command-lines). Each command-line starts you with the dollar sign ($). One can change this: in some
others the prompt ends with >. This prompt means that the shell is ready to accept your typed commands.
Every user has a unique username. Bash is the default Unix shell on may GNU/Linux distributions (e.g.,
Debian, Fedora, Ubuntu).
When you logon to the system, you are placed in a home directory, which is a portion of the disk space
reserved just for them.
Unix commands are strings of characters typed in at the keyboard. To run a command, you just type
it in at the keyboard and press the ENTER key. We will look at several of the most common commands in
the following sections.
Unix extends the power of commands by using special flags that are usually preceded by a dash ( - ) and
preceed any filenames or other arguments on the command line. Unlike the DOS (or Windows) command
line, Unix systems are case sensitive (upper and lower case characters are considered different). Nearly all
command names and most of their command line switches will be in lowercase.
• Shell: The shell is the utility that processes your requests. When you type in a command at your
terminal, the shell interprets the command and calls the program that you want. The shell uses
standard syntax for all commands. C Shell (csh), Bourne again Shell (bash) and Korn Shell (ksh)
are the most famous shells which are available with most of the Unix variants.
• Commands and Utilities: There are various command and utilities which you would use in your
day to day activities. cp, mv, cat and grep etc... are a few examples of commands and utilities. There
are over 250 standard commands plus numerous others provided through 3rd party software. All the
commands come along with various optional options.
• Users: Unix system is based on users and privileges. The main user with which one manages the
system is called root (or super user su). Then each person who will work in the system has a user
with less privileges than root. Doing this, any user can damage the system (so, do not panic!). On
top of that there are groups and all users sets of users. With that one can manage who has access to
certain files and directories, rights to modify files, etc... root is ‘the one to rule them all’
• Files and Directories: All data in Unix is organized into files. All files are organized into directories.
These directories are organized into a tree-like structure called the filesystem. Files and directories
have assigned modes, with which one manages the rights to execute, read and modify of the owner
(user) of the file, group and all users in the computer (see further on for a detailed explanation).
1
UNIX (capitalised) is the operating system created by AT&T at Bell Labs in the 1970s. Derivative versions are denoted
by Unix, but here we’ll use that term for any Unix-like operating system. Among these Unix-like operating systems one of the
most popular is GNU/Linux. GNU stands for GNU’s Not Unix : indeed, it is not Unix as it does not use any of the original
codebase of the AT&T UNIX code. It is rather written from scratch with the goal of providing an operating system consisting
of only free software. On purpose the GNU system is similar to UNIX and henceforth in this document referred to as Unix.
IPSL BootCamp: 2016/03/24. Unix commands 3
2 Before starting
2.1 Common vocabulary
Common language and basic knowledge to navigate through Unix commands:
• Command-prompt: At the beginning of the command-line, the command-prompt looks like: user@computer:
It indicates that the computer is ready to accept commands and provides useful information when
working with multiple remote computers at the same time.
• Hidden files: Files whose first character is the dot or period character (.). Unix programs (including
the shell) use most of these files to store configuration information. Common hidden files:
• Meta characters: Meta characters have special meaning in Unix. For example * and ? are metachar-
acters.
• Command manual: man command1 displays on-line reference manual pages, in the example for
command1:
man -k keyword → List the manual page subjects that have keyword in their headings. This is
useful if you do not yet the name of a command you are looking for.
Up & down arrows → Move back (↑) and forward (↓) in the history of commands you typed, or
use the command history that lists previously executed commands.
IPSL BootCamp: 2016/03/24. Unix commands 4
• whoami → This returns your username. You may need to you have logged out your username;
• uname → Get complete information on your Operating System, use option -a: uname -a;
• passwd → Change your password, which you should do regularly (at least once a year).
df -h → Print free space in human readable format (e.g., 1K, 234M, 2G, etc.) (flag -h).
• du → Show disk space used by files or directories (without argument the current directory is analyzed);
du -hs dirname → Show disk space used by dirname in human readable format and without
specifying disk space used by all sub-directories (flag -s).
• quota → Show what your disk quota is (i.e. how much space you have to store files), how much you’re
actually using, and in case you’ve exceeded your quota (which you’ll be given an automatic warning
about by the system) how much time you have left to sort them out (by deleting or compressing some,
or moving them to your own computer).
.. Parent directory;
• cd dirname → Change directory and move in dirname. dirname may be either the full pathname of
the directory, or its pathname relative to the current directory.
• Owner permissions: The owner’s permissions determine what actions the owner of the file can
perform on the file.
• Group permissions: The group’s permissions determine what actions a user, who is a member of
the group that a file belongs to, can perform on the file.
• Other (world) permissions: The permissions for others indicate what action all other users can
perform on the file.
$ ls -l /home/amrood
-rwxr-xr– 1 amrood users 1024 Nov 2 00:10 myfile
drwxr-xr–- 1 amrood users 1024 Nov 2 00:10 mydir
Here first column represents different access mode, i.e. permission associated with a file (- as first
character in the first column) or directory (d as first character in the first column). The permissions are
broken into triples, and each position in the group denotes a specific permission, in this order: read (r),
write (w), execute (x):
• Owner The first three characters (2-4) represent the permissions for the file’s owner. For example,
-rwxr-xr-- represents that onwer has read (r), write (w) and execute (x) permission.
• Group The second group of three characters (5-7) consists of the permissions for the group to which the
file belongs. For example, -rwxr-xr-- represents that group has read (r) and execute (x) permission
but no write permission.
IPSL BootCamp: 2016/03/24. Unix commands 6
• Other The last group of three characters (8-10) represents the permissions for everyone else. For
example, -rwxr-xr-- represents that other world has read (r) only permission.
File Access Modes: The permissions of a file are the first line of defense in the security of a Unix system.
The basic building blocks of Unix permissions are the read, write, and execute permissions, which are
described below:
2. Write: Grants the capability to modify, or remove the content of the file.
1. Read: The user can read the contents and can look at the filenames inside the directory.
2. Write: The user can add or delete files to the contents of the directory.
The execute bit clearly has a different meaning than that of a normal file. Try this:
Change permission
chmod {options} filename → Change the read, write, and execute permissions on your files.
chmod o+r filename → Make the file readable (flag r) for everyone (flag o). The plus symbol adds
the named right.
chmod o-r filename → Make it unreadable for others again. The minus symbol removes the named
right.
chmod 755 filename → Using numerical format the three number give permissions to, in order: the
owner, the group, and others. Number 7 corresponds to all permission (read, write and execute),
while 5 provides only the rights to read and execute (not to write). See at the following link, for more
information: https://en.wikipedia.org/wiki/Chmod.
IPSL BootCamp: 2016/03/24. Unix commands 7
4.2.3 Files
About filenames in Unix: A filename in Unix can consist of any combination of characters on the keyboard
except for the null character and slash (/) as these are control characters. Moreover, on modern Unices
typically any Unicode character is allowed. You are adviced not to use the following characters in filenames:
* ? ! | \ / ’ " { } < > ; , ^ ( ) $ ~. These characters can be used in filenames, but only by escap-
ing (prefixing with a backslash) or quoting them because they have special meaning to the shell. On Unix
systems, the extension of a filename (e.g.: *.pdf, *.png, *.txt) are part of the filename. This does not
need to to define the type of file; for that use file(1):
$ ls -l
total 105632
-rw-r–r– 1 mvhulten lsce 1040 Mar 23 13:17 README
-rw-r–r– 1 mvhulten lsce 108092785 Mar 23 13:13 rms_gplv3_launch.ogg
-rw-r–r– 1 mvhulten lsce 40541 Mar 23 13:12 sound.ogg
-rw-r–r– 1 mvhulten lsce 26398 Mar 23 13:18 unix.tex
$ file *
README: ASCII text
rms_gplv3_launch.ogg: Ogg data, Theora video
sound.ogg: Ogg data, Vorbis audio, stereo, 48000 Hz, 192000 bps
unix.tex: LaTeX 2e document, UTF-8 Unicode text, with very long lines
When using ls -l you only see the permission, size and date of change of the files. The extension does not
help you much in identifying the filetype: README does not have an extension, and, while in this case both
.ogg files contain Ogg data, you cannot see if they contain audio or video streams. Only when using file
FILES you see the type of file. Only now you are comfortable executing these commands:
• more filename → Show the first part of a file, just as much as will fit on one screen. To see more,
just hit the space bar. To left, type q. To search for a pattern, use /pattern;
• head filename or tail filename → show the first or last ten lines;
• wc filename → Count how many lines, words, and characters there are in a file. The command
output lists, in order, the number of lines, words, and characters. To restrict the count to: (1) lines,
use flag -l, (2) words, use flag -w, or (3) characters, use flag -c.
To handle files:
• cp filename1 filename2 → Copy the content of filename1 into filename2. cp cannot copy a file
onto itself.
IPSL BootCamp: 2016/03/24. Unix commands 8
cp filename1 filename2 dirname1 → Create copies of filename1 and filename2 (with the
same names), within the directory dirname1, which must already exist for the copying to succeed.
cp -r dirname1 dirname2 → Recursively copy the directory dirname1, together with its con-
tents and subdirectories, to the directory dirname2 (flag -r). If dirname2 does not already
exist, it is created by cp, and the contents and subdirectories of dirname1 are recreated within
it. If dirname2 does exist, a subdirectory called dirname1 is created within it, containing a copy
of all the contents of the original dirname1. dirname1 and dirname2 can be either directories or
full pathnames of directories.
• mv filename dirname/ → Move a file into a specified directory. dirname can be either a directory
or a full pathname of the directory
• rm -i filename → Remove a file. It is wise to use the flag -i, which will ask you for confirmation
before actually deleting anything.
• diff filename1 filename2 → Compare files and show where they differ
diff filename1 filename2 > diff.txt → Print differences between the two files in a text file,
named diff.txt. The symbol > redirects the output of a command in a file, instead of showing
results on screen.
• find → Search for files in a named directory and all its subdirectories:
find . -name *.f -print → Look for all files ending with .f in the current directory and all
its subdirectories, and write their names to the standard output (i.e., on screen);
find /local -name core -user user1 -print → Search the directory /local and its subdi-
rectories for files called core belonging to the user user1 and writes their full file names to the
standard output.
• grep → Search for lines in files containing a specified pattern/string and, by default, writes them to
the standard output. grep is CASE sensitive. This can be useful: e.g. finding the right file among
many, figuring out which is the right version of something. grep has a lot of very flexible options.
• zip
IPSL BootCamp: 2016/03/24. Unix commands 9
– zip filename → Compress and produce files with the suffix .zip appended to the original
filename;
– unzip filename.zip → Uncompress a file .zip.
– gzip filename → Compress and produce files with the suffix .gz appended to the original
filename;
– gunzip filename.gz → Uncompress a file .gz.
Compression works better if files are combined and then compressed together, rather than compressing
them individually, since this allows the compression program to spot repeated patterns between the files.
For this need, you should use the Unix tool for packing and unpacking files and create archives: tar.
Pack into a single tar file, all files ending with the suffix .txt:
tar -cvf pack.tar *.txt
Flags: c means create, v shows on screen files that are packed, f means that the filename (pack.tar)
to write to is specified. Then you may use gzip(1) to compress the tar ball : gzip pack.tar To pack and
gzip sequences simultaneously, add flag z to the tar command:
tar -zcvf pack.tgz *.txt
.tgz is just short for .tar.gz. To extract use flag x:
tar -xf pack.tar
tar -zxf pack.tar.gz
To list the contents without extracting, use flag t:
tar -ztf sequences.tgz
To extract a single file from your tar:
tar -xf pack.tar filename
or, using long-opts:
tar --extract --file={pack.tar} {filename}
6.2.2 rsync
This command is very handy to synchronize files and directories between two different machines. Its use is
very similar to that of scp
rsync [flags] [email protected]:[source files] [email protected]:[location]
rsync does not change metadata of the files (date of creation or last modification). By default rsync(1)
overwrites any existing files in the destination directory. If you supply the --update with rsync(1), it only
copies those files and directories that have changed if it finds the same files on the destiny machine.
6.2.3 ftp
The ‘File Transfer Protocol’ is the basic way to transfer files within the internet. It is based on server/client
infrastructure. When one is connected with ftp to a server, a session is opened there mainly to navigate
within the disk space (commonly a dedicated space into the server), copy (via get) and upload (via mput)
files among a full standard set of file related instructions.
See https://en.wikipedia.org/wiki/List_of_FTP_commands for a full set of commands.
6.2.4 wget
This command is commonly used to get from a file from the internet. It supports http, https and ftp. It
can resume a failed file retrieval, and use wildcards among other things. Its syntax is at follows:
wget internetsourcelink
7 Packages
Unix system distributions are based on a series of packages and applications. These can be installed in the
system. Assuming you run a Debian- or Redhat-based GNU distribution, they are managed via one of two
main applications: apt or yum. Commonly only root manages the applications installed in the machines.
Because in almost all the IPSL laboratories there are dedicated computer manager technicians, we will not
extend more on this point.
Some of the most popular applications:
• OfficeSuite: libreoffice
8 A few links
• https://swcarpentry.github.io/shell-novice/reference.html