Table of Contents
Directory structure
In Unix, the entire file system is visible in the form of directories located in the virtual directory “/”. We will find here catalogs such as:
- / bin – containing basic console commands
- / etc – containing system configuration files
- / dev – containing device files
- / lib – containing basic system libraries
- / sys – containing system files
- / usr – containing files and programs intended for users, on some systems, the user’s home directories are also placed.
- / var – containing, among others system logs and files used by so-called Daemons, i.e. applications that provide system services (mail, printers, etc.)
- / tmp – temporary files directory
In Linux, you’ll also find:
- / root – administrator’s home directory
- / sbin – commands for administrator use
- / proc – a virtual directory containing virtual files with system and computer status information
- / boot – containing, among others information for the kernel launcher (LILO, Grub)
- / home – directory containing users’ home directories
The / usr directory contains a similar structure as / – we can find the / usr / bin, / usr / lib, / usr / etc directories, etc. Usually the / most important programs and applications are placed in the / directories and the / usr subdirectories – the others.
System Help
Virtually every command has an option –help that displays information about the syntax and available options, e.g .
$ ls --help
For most commands there are so-called system manual pages. writing:
$ man command
should display a man page about the given command. However, please be aware that the manual also includes, for example, C language command descriptions and system function descriptions. A distinction is made between chapter number commands by writing:
$ man chapter_number command
We can get short information about a given command by writing:
$ man command
We will get information about commands containing a string in the name by typing:
$ apropos string
The most detailed information can be found on the so-called info pages. Just write:
$ info command
To exit the manual or info browser, simply press q.
Commands related to files and directories
pwd
If no information about the current directory appears at the prompt, you can display its name by typing:
$ pwd
cD
To change the current directory, use the cd command with the syntax:
$ cd directory_name
Note: it is worth remembering that in the bash shell pressing the tab stops the file names. This allows you to issue commands faster.
If we don’t specify a parameter, the cd command will take us to our home directory. ( Note: this is a significant difference from DOS / Windows – there the cd command without parameters worked like pwd). If we want to go to the parent directory, we write:
$ cd ..
ls
If we want to print the contents of a given directory, we simply write:
$ ls directory_name
If we omit the name, ls will list the contents of the current directory. This command has many parameters, among the most important are:
-a
shows all files, including “hidden” (ie, names beginning with a
dot)-l
prints detailed information about files (owner, rights, modification dates,
size)
Options with one minus and one letter can be combined – ” ls -a -l
” corresponds to ” ls -al
“.
An interesting option is --color
– if the given version of ls supports it, different types of files are suitably colored (otherwise directories, different ordinary, executable, multimedia files, etc.). If this option is enabled by default and you do not want to color (useful for example in the for loop) – set it to none ( --color=none
).
rm
To delete files, use the rm command:
$ rm filename
Instead of the file name, you can use the symbols *
and ?
, Then you should force confirmation to delete each file:
$ rm -i abc *
rmdir
To remove empty directories, use the rmdir command:
$ rmdir directory_name
If the directory contains files or directories – it will not be deleted. To remove directories and content, we use the appropriate rm command options:
$ rm -rf directory_name
mkdir
If we want to create a new directory, we use the mkdir command:
$ mkdir directory_name
If we want to create an entire directory tree, we use the option -p
:
$ mkdir -p once / two / three / four
We can also provide permissions for the created directory:
$ mkdir directory -m 770
A directory will be created with read, write and execute permissions for the owner and group, and no permissions for other users.
mv
If we want to move the file to another directory, we use the mv command:
$ mv file directory
We do the same when changing the file name:
$ mv old_name new_name
Note: if the file new_name exists – it will be overwritten without warning. Warnings can be enabled with the option -i
. Another solution is to back up the overwritten file. Recommendation:
$ mv -b one two
will rename the two files: ” dwa
” to ” dwa~
“, and ” jeden
” to ” dwa
“.
We can simultaneously move the file to the directory with the name change. If we want to move from the current directory the file ” abc
” to the directory ” katalog
“, changing its name to ” xyz
” we write:
$ mv abc directory / xxx
Adding the ” -i
” option will cause mv to ask before overwriting files.
cp
To copy the file, use the cp command:
$ cp source_file target_file
To copy a directory and its contents:
$ cp -R source_directory destination_directory
Similar to mv, we can order confirmation when overwriting files – option ” -i
“.
cat
To write the contents of the file to the screen, we use the cat command:
$ cat file
chmod, chown and chgrp
Use the chmod command to grant file permissions. There are two variants of this command. In the first we give the permissions as a series of digits:
$ chmod file 752
The first number is the permissions for the file owner, the second is the permissions for the group in which the owner is located, and the third is the permissions of other system users. Individual digits are the result of adding individual rights: 1 corresponds to the right x (exercise); 2 – in (write); 4 – r (read).
So 752 is:
- the right to exercise, write and read to the owner;
- group exercise and write rights;
- write permission for others.
The second way is to write “to whom what”, e.g .:
$ chmod u + rwx filename
If we have administrator rights, we can change the owner of the file:
$ chown other user file
If we are an administrator or have membership in many groups, we can also change the ownership group:
$ chgrp other_group file
Home directories
How do you refer to your own home directory if you do not know where it is located? There are two ways:
- the current user’s home directory is always available as ”
~
“. - the current user’s home directory is contained in a variable
$HOME
.
So these commands are equivalent to:
$ cd ~
$ cd $ HOME
$ cd
How do you refer to someone’s home directory? We precede the username with the tilde symbol ” ~kasia
“. Then we do not need to know if Barry is in your home directory /home/kasia
, /usr/kasia
, /Users/Kasia
, /usr/users/grupy/grupa1/kasia
etc.
Hard and symbolic links (“hardlinks” and “symlinks”)
To create a hard link to a given file, type:
$ ln filename link_name
A second entry pointing to the file will then be created in the inode table.
To create a symbolic link, type:
$ ln -s filename link_name
In this case, the “link_name” text file will be created containing the path to the file “filename”. This file will get an additional “l” authority, meaning it is an appeal.
Why are symlinks better than hardlinks? Because by listing the contents of the directory, we can immediately see that the file is a link:
$ ln -s symlink file
$ ln hardlink file
$ ls -l
together 1
-rw-r - r-- 2 marcoos users 0 2003-12-17 23:28 hardlink
-rw-r - r-- 2 marcoos users 0 2003-12-17 23:28 file
lrwxrwxrwx 1 marcoos users 4 2003-12-17 23:28 symlink -> file
Data archivization
To handle archived files, the tar command is primarily used:
Extract the .tar file in the current directory:
$ tar -xf file.tar
Creating an archive with the contents of the directory:
$ tar -cf archive.tar directory
Please note that no compression is performed, only a file containing other files and location information is created.
Compressing the file to the gzip format (“file.gz” is created):
$ gzip file
Decompressing the .gz file:
$ gunzip file.gz
Compression and decompression to the bzip2 format (bzip2, bunzip2) is analogous.
You can also immediately create compressed tar archives:
$ tar -czf archiwum.tar.gz directory
$ tar -cjf archive.tar.bz2 directory
and decompress and unpack such archives:
$ tar -xzf archive.tar.gz
$ tar -xjf archive.tar.bz2
During decompression, you can now omit the parameters -z
and -j
, tar will automatically detect what type of compression it is dealing with.
Disk support
On Unix systems, always mount the disk, floppy disk or disk to the directory. This is done with the mount command:
$ mount -t file_system_type / dev / device / mnt / directory
If the system is properly configured, all you need is:
$ mount / mnt / directory
After working with the disk, please unmount it by:
$ umount / mnt / directory
or
$ umount / dev / device
By default, the mount command attempts to mount the drive in read and write mode. If we want to mount a read-only disk, then we should add ” ro
” to this command .
Example: Installing a FAT format diskette in the first disk drive, read-only:
$ mount -t vfat / dev / fd0 / mnt / floppy ro
unmount:
$ umount / dev / fd0
Example 2: Mounting the CD-ROM:
$ mount -t iso9660 / dev / cdrom / mnt / cdrom ro
Note: Whether a normal user has the right to mount disks depends on the system administrator.
Output pipes
Sending command results to a file with file overwriting …:
$ command> file
… and without overwriting, appending to the end of the file:
$ command >> file
Sending command results to the “results” file and errors to the “err” file:
$ command >> results 2 >> err
Input pipes
Getting values from the file that will be given to the command input:
$ command <file
Giving command2 the results of command1:
$ command1 | command2
Example:
ls -al | loess
The less command is an improved version of the more command, which divides the output of the command into pages that fit on the screen. The next page is a space, you can use the cursor keys and PgUp, PgDn, Home, End.
Symbol “<<“
Let’s assume that we want to pass some multi-line text to the standard input of a command, which we are just going to enter from the keyboard. How to do it? Let’s use the symbol “<<“, placing a tag just before (usually spelling “EOF”):
$ cat> file << EOF
> In Paris
> the best chestnuts
> are in the square
> Pigalle
> EOF
$ cat file
In Paris
best chestnuts
are in the square
Pigalle
We have created a file here called “file” with the content that we entered in the following lines from the keyboard.
Search in files and files
To print out all lines of the file.txt containing a given word, type:
$ grep word file.txt
However, this command will print lines not containing this word:
$ grep -v word file.txt
If we want to find out where the executable is located (available thanks $PATH
), we use the whereis command:
$ whereis vi
vi: / usr / bin / vi
If we want to search the file system according to a given criterion, we use the find command. General Syntax:
$ find where_ search for what_criterion after_following
Some examples:
find -name "html" -print
It will search for all files in the current directory with names containing the word “html”.
find -name '* .c' -ok less {} \;
It will search for all .c files, for each found it offers to display its contents on the screen.
find -name '* .c' -exec less {} \;
Similarly, but immediately displays files without confirmation.
find / tmp -atime +3 -and -uid +499 -print -exec rm -rf {} \;
This will delete all files that have not been used for 3 days and their owners are users with a UID greater than 499 and found in all files from the / tmp directory, information about the files found will be transferred to the monitor.
find / usr / src -type f -exec grep "stdio.h" {} \; -print
It will search for all files in the / usr / src directory containing ” stdio.h
” in its content . The file name will be printed after all lines from the given file containing ” stdio.h
“.
Work with processes
Usually, bash work is sequential – we execute command by command. But UNIX is a multi-tasking system and sometimes we would like to run a program in the background. In this situation, we use the symbol “&”. For example, we would like to use the mpg123 command to listen to the music file on the console, but at the same time have the command line available:
$ mpg123 song.mp3> / dev / null 2> / dev / null &
$ another_commands
What have we done here We launched the mpg123 program playing mp3 music files in the background, redirecting to the device / dev / null (“black hole”) the text output of this program (so that it does not clutter the screen with its messages). Thanks to this, the music plays in the background while we can perform further commands on the console.
What if we run a program that takes a long time and blocks the console to us? Press CTRL-Z and the program will be paused (not closed). If we give the command now:
$ bg
the program will be “frostbitten”, but will continue to run in the background.
However, if you enter:
$ fg
we will return to this program, the console will be locked again.
Each process on UNIX has its own identifier (number), so-called PID (process identifier). Thanks to this identifier, both the system and the user can manage implemented processes to some extent.
To see the list of processes running on the current console, write:
$ ps
PID TTY TIME CMD
6092 pts / 1 00:00:00 bash
7304 pts / 1 00:00:00 mpg123
7305 pts / 1 00:00:00 ps
We see that bash has PID 6092, and mpg123 – 7304. If we would like to know the command that a given process was running, we would write:
$ ps x
We list all “our” processes with the command:
$ ps u
The ps command has many more options. We’ll get a short list by writing ” ps –help
“, it’s also worth looking through the manual and info pages.
To immediately stop the process of a given PID, write:
$ kill -9 PID
To interrupt all processes with a given name, write, for example:
$ killall -9 mpg123
These commands send various signals to the process, most often they are signals that force the process to end. Signal 9. (KILL) is the most effective – it causes the immediate termination of the process. It is more elegant to send a 15. signal (TERM), but we don’t always want to do it. There can be many signals of this kind, and the behavior of a given process to a given signal can also be different (except 9). Often, e.g. signal 1 (HUP) is used as an order to re-read configuration files by a given process.
We will receive a list of available signals by writing:
$ kill -l
A process that is not running in the background (blocking the console) can often be terminated by simply pressing Ctrl-C.
Environment Variables
The significance of some environment variables is shown below.
$PATH
– contains a list of directories in which commands are searched$SHELL
– contains the name of the current shell (e.g. “/ bin / bash”, “/ bin / csh” etc.)$USERNAME
– contains the name of the current user$HOSTNAME
– hostname$HOME
– contains the current user’s directory name$PS1
– main prompt$PS2
– second prompt$CC
– contains the name of the default C compiler$CXX
– contains the name of the default C ++ compiler$LC_ALL
– (in simplified terms) contains the current user’s language settings.
If LC_ALL is set to ” en_In
“, the system will communicate with us in English (as long as English localization packages are installed). To return to the English-language console, set LC_ALL to ” en_US
“:
Example:
$ LC_ALL = "en_In"
$ ls nonexistent file
ls: nonexistent file: There is no such file or directory
$ LC_ALL = "en_US"
$ ls nonexistent file
ls: nonexistent file: No such file or directory
A detailed description of all environment variables can be found in the bash (man bash) shell system manual.
aliases
If we often use a command that is long or has uncomfortable syntax, we can create a so-called alias. Here is an example:
$ alias mountsample = "mount -t vfat / dev / fd0 / mnt / floppy ro"
If we want to delete this alias, we write:
$ unalias mountsample
For the shell to remember aliases, you must append them to .bashrc or .bashprofile. To view the list of aliases, issue the alias command without parameters.
Information about users
who
– displays the list of logged in users, console name and the time from which they are logged inwhoami
– print our usernamewho am i
– displays information about us in a way like “who”
User account settings
If we don’t want bash to be our default shell, we can change it:
$ chsh -s / bin / some_shell
To change the password for our account, we use the command:
$ passwd
Files with a dot in the name
Several files with names beginning with a dot are important for the bash shell. Here’s what they are for.
~/.bashrc
– This script is executed when the shell is started manually.~/.bash_profile
– Script to be run when the login shell is initialized (i.e. started when logging in, not manually)~/.bash_logout
– Script to be run when exiting the shell~/.bash_history
– The file contains the history of our commands