Comparaison des versions

Légende

  • Ces lignes ont été ajoutées. Ce mot a été ajouté.
  • Ces lignes ont été supprimées. Ce mot a été supprimé.
  • La mise en forme a été modifiée.

If you would like to look at a book I suggest:
    www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf

HOME

By default your HOME directory is your working directory the directory in which you "land" when you first log in. The pathname path of this directory is usually:
     /home/username
It is also stored in the HOME environment variable (see further down) variable:
    $      echo $HOME

In general there is not much space in your HOME but therefore, at UQAM, a backup of everything you keep under your HOME is done every night and kept for a week. The backup of Friday nights is even kept for a month.
You if you ever remove something by accident or if the machine/disk crashes, there will still be a copy of everything you had in your HOME.
Therefore it is strongly suggested that you keep everything which is small and importent in your HOME! Like programs, scripts, configuration files.
If ever you need to get something back from the backup, talk to Nadjet Labassi.
If you need to store larger amounts of data, talk to Katja Winger.

Shell

A shell is a UNIX system command processor. It's a command language to tell the system what to do. There are two major shell (command interpreter) families: 
    Bourne, Korn, Bash Shell
    C Shell
   
The syntax of UNIX commands can vary from one shell to another.
At UQAM we use the Bourne Again Shell (bash) for the environment but often Korn Shell (ksh) for scripting. Therefore all commands below are bash commands.

Anatomy of a Unix Command

command-name [-option(s) filename(s) or arguments]

Everything in square brackets [] is optional.

Example: wc ls -l sample filename

The first word of the command line is usually the command name. This is followed by the options/keys, if any, then the filenames, directory name/names, or other arguments, if any, and then a RETURNfile and/or directory names. Options are usually preceded by a single or double dash, '-' or '--', and you may use more than one option per command.

You can also most of the time combine options:     $  ls -l -t -r
is the same as
    $  ls -lrt

In case you have a file name or an argument starting with a '-' you can use '--'. The command will NOT interpret anything following a '--' as an option/key.
For example if you are using 'grep' to look for '-a' in a file you could type:
    $  grep -iw -- -a filename

For more detailed explainations explanations about Program Argument Syntax Conventions, please click on the here.

...

On most systems your home quota (the amount of data you can keep under your home) is limited. The following commands help to check quotas and sizes of directories and file systems:

quota -v display your disk quota and usage.
There are quotas on several of our file systems. You might want to check them once in a while.
du -sdisplay your total disk usage
du -sc *display the size of all files and folders in current directory.
Very useful if you need to do a clean up because your quota is exceeded!
df -hcheck if the file systems are full - Do NOT do this on guillimin!
echo $PATHinspect your search path


List Files and Directories

pwd : display the name of present working directory

true_path directory-name : display the "true" name of directory-name, not the links. This command is a local add-on.

ls directory-name : list contents of directory

Here are some of the options I find most useful:

    -alist all files including invisible files (starting with '.')

-llong list - shows ownership, permission, and links

-hlist size in human readable format (k-, M-, GBytes)

-tlist files chronologically

-rreverse order of listing

-ddo not display directory contents but only the directory itself

-Slist in order of size (helps to find the largest files)


Understand the "long list (-l)" output:

...

    $ ls -ld /home/winger
    drwxr-xr-x   20   winger   users   4096   May 24 08:56   /home/winger

Column 1  :   a set of ten permission flags (see below)
Column 2  :   link count (don't worry about this)
Column 3  :   owner of the file
Column 4  :   associated group for the file
Column 5  :   size in bytes
Column 6-8: date of last modification (format varies, but always 3 fields)
Column 9  :   name of file/directory/link (possibly with path, depending on how ls was called)

The permission flags are read as follows (left to right)

positionMeaning
1directory flag, 'd' if a directory, '-' if a normal file,
something else occasionally may appear here for special devices.
2,3,4read, write, execute permission for User (Owner) of file
5,6,7read, write, execute permission for Group
8,9,10read, write, execute permission for Other



valueMeaning
-in any position means that flag is not set
rfile is readable by owner, group or other
wfile is writeable. On a directory, write access means you can add or delete files
xfile is executable (only for programs and shell scripts - not useful for data files).
Execute permission on a directory means you can list the files in that directory
sin the place where 'x' would normally go is called the set-UID or set-groupID flag.


See 'chmod' below about how to change the permissions.

...

Look at a ASCII/text File

cat filenamedisplay the whole file contents one screen
more filenamedisplay the file contents one screen at a time
less filenameprogram for browsing or paging through files or other output.
Can use arrow keys for scrolling forward or backward.
head filenamedisplay first 10 lines of a file. Option -n : display first n lines
tail filenamedisplay last 10 lines of a file. Option -n : display last n lines


Wild cards

A number of characters are interpreted by the Unix shell before any other action takes place. These characters are known as wildcard characters. Usually these characters are used in place of filenames or directory names.

*An asterisk matches any number of characters in a filename, including none.
?The question mark matches any single character.
[ ]Brackets enclose a set of characters, any one of which may match a single character at that position.
-A hyphen used within [ ] denotes a range of characters.
~A tilde at the beginning of a word expands to the name of your home directory. If you append another user's login name to the character, it refers to that user's home directory.



Here are some examples:

  1. cat c* : displays any file whose name begins with c including the file c, if it exists.
  2. ls *.c : lists all files that have a .c extension.
  3. cp ../rmt? . : copies every file in the parent directory that is four characters long and begins with rmt to the working directory. (The names will remain the same.)
  4. ls rmt[34567] : lists every file that begins with rmt and has a 3, 4, 5, 6, or 7 at the end.
  5. ls rmt[3-7] : does exactly the same thing as the previous example.
  6. ls ~ : lists your home directory.
  7. ls ~hessen : lists the home directory of the guy with the user id hessen.

...

You can create variables and assign values to them.
There are two kinds of variables: local variables and environment variables.
The difference between the two is that environment variables are passed down to child processes (sub shell, scripts, programs)
There are already several environment variables set which you can see with:
    env

A variable name always needs to start with a letter.
To get the content of a variable the variable needs to be preceded with a dollar sign ($)

Examples:
    $  person=Alex     Never put a space before or after the equal sign!
    $  echo person
    person
    $  echo $person
    Alex
    $  echo $personis
   
    $  echo ${person}is
    Alexis
    $  echo "$person"
    Alex                     => double quotes evaluate
    $  echo '$person'
    $person                => single quotes: No evaluation done !!!

...


Environment variables and the "export" command

If you just set 'variable=word' the variable will only be known in the current shell/script in which it is set.
If you "export" the variable becomes an environment variable and will also be know in all scripts which get executed in this shell/script.
For example ifyou export a variable in your file '~/.profile_usr' resp. '~/.profile.d/.interactive_profile' it will be available in all sub shells.

export
variable=word

The value of the variable is set to word.

Example:
    $ export CMCLNG=english
or
    $ CMCLNG=english
    $ export CMCLNG
   

...

rsync : a fast, versatile, remote (and local) file/directory-copying tool

Usage:
  rsync  origin  destination
  rsync  user@origin_machine:origin  destination
  rsync  origin  user@destination_machine:destination
origin and destination can be file(s) and/or directories.
Only the origin or the destination machine can be a nother machine, not both.

Options:

    -vverbose

-rRecursively copy entire directories

-uupdate, forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file.

-llinks, when symlinks are encountered, recreate the symlink on the destination

-Lcopy-links, When symlinks are encountered, the item that they point to (the referent) is copied, rather than the symlink.

-tpreserves time

-ppreserves permissions



scp
: secure copy (remote file copy program)

Usage: similar to rsync above

Options:

   -rRecursively copy entire directories. Note that scp follows symbolic links encountered in the tree traversal.


Also see:
sftp
: secure file transfer program
ftp : Internet file transfer program

wget : web downloader
GNU Wget is a free utility for non-interactive download of files from the Web.
Example:
    $  wget www.tldp.org/LDP/Bash-Beginners-Guide/Bash-Beginners-Guide.pdf


...

tar : tape archive, can create, add to, list, and retrieve files form an archive file (a collection of files archived together as one file).
Options:

    -vverbose

-ccreate archive

-ttable of contents

-xextract files

-ffollowed by name of tar-file                                    

-tpreserves time


Examples:
Create tar-file:   tar -cvf tar-filename.tar  filenames
Unarchive tar-file:   tar -xvf tar-filename.tar

gzip, gunzip : compress resp. expand files

    gzip filename : compresses the file filename. Whenever possible, each file is replaced(!) by one with the extension .gz
    gunzip filename.gz : uncompresses the gz-file again.
Good to save space!

zip, unzip : package and compress resp. unarchive and uncompress (archive) files

    zip zip-file.zip file_list: archives and compresses all files in file_list into zip-file.zip. Leaves original files untouched.
    unzip zip-file.zip : unarchives and uncompresses all files in zip-file.zip.
    unzip zip-file.zip aaa : unarchives and uncompresses only file 'aaa fromzip-file.zip.

...

Example string:
     $ string="ABC_abc_123"

String length:
     $ echo ${#string}

#      removes minimal matching prefixes
##   removes maximal matching prefixes
%     removes minimal matching suffixes
%%  removes maximal matching suffixes
:s:n  get 'n' caracters starting from position 's' (first position is 0)

Look at these examples and you will understand the meaning of the above:
    $ string="ABC_abc_123"
    $ echo ${string#*_}     
    abc_123                                             => removes everything before first occurance of '_'
    $ echo ${string##*_}
    123                                                    => removes everything before last occurance of '_'
    $ echo ${string%_*}
    ABC_abc                                            => removes everything after last occurance of '_'
    $ echo ${string%%_*}
    ABC                                                   => removes everything after first occurance of '_'
    $ echo ${string:s:n}
    abc                                                    => gets n characters, starting from position s

Cut out part of the content of a variable:

cut prints selected parts of a string
Options:

    -cselect only these characters

-ddelimiter=DELIM; use DELIM instead of TAB for field delimiter

-fselect only these fields


Examples:
    $ abc="I am hungry"
    $ echo $abc | cut -c 6-9
    hung                                                => returns characters 6-9
    $ echo $abc | cut -c 6-
    hungry                                            => returns characters 6 to end
    $ echo $abc | cut -d' ' -f 3
    hungry                                            => returns 3rd element with ' ' used as seperator
    $ abc="I am hungry. Are you?"
    $ echo $abc | cut -d. -f 2
    
Are you?                                        => returns 2nd element with '.' used as seperator

set
When you call set without options but with one or more arguments, it sets the values of the command line argument variables ($1-$9) to its arguments.
Examples:
    $ abc="I am hungry"
    $ set $abc
    $ echo $3
    hungry

When options are specified, they set or unset shell attributes.
Options:

    -xexpands each simple command
This is very useful for debugging scripts!!!

+xno expanding of commands anymore

For more options check man page: man set
Examples:
    $ abc="I am hungry"
    $ set -x
    $ abc="I am hungry"
    + abc='I am hungry'


...

ps reports a snapshot of the current processes
Options:

    -eselect all processes

-fdo full-format listing

-u userlistthis selects the processes whose effective user name or ID is in userlist


Try for example:
    $ ps -fu $USER

If ever you need to kill one of these jobs you can use 'kill' followed by the 'PID'.
If a normal 'kill' does not work try 'kill -9' followed by the 'PID'.