introduction to unix/linux commands. commandline interface text console mode usually a black screen...
TRANSCRIPT
Introduction to Unix/Linux Commands
Commandline Interface
Text console mode Usually a black screen with white text All commands are typed in You cannot use a mouse You must hit <enter> after every command to activate
the command To logout, type logout<enter> or hit <ctrl>d
“A Unix shell is a command-line interpreter that provides a traditional user interface for the Unix operating system and for Unix-like systems.”
“Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands.”
from wikipedia
Shells create processes that execute user commands.
SHELLS
most popular:csh (%)bash ($)tcshksh
SHELLS
You can also see the shells supported on your machine by typing cat /etc/shells$ cat /etc/shells# /etc/shells: valid login shells
/bin/sh /bin/dash /bin/bash /bin/rbash
To switch to another shell, just type in the name of the shell
bash <enter>
To go back to your original shell, enter <ctrl>d, logout, or exit
To see which shell you are using, enter echo $SHELL
When you log in, the shell will load your configuration file (if present). It "sets up" your shell environment.
These files start with a period (.) and should be located in your home directory.
These files are not listed by ls by default. (They are “invisible.”) To have ls list them, use the –a option.
Use .cshrc for csh; use .bashrc for bash. .cshrc is executed every time a new c-shell is started (and is
executed first when you login)
CONFIGURATION FILES
EXAMPLE .CSHRC
Environment variables
The settings and values that control the way you work on the system.
Environment variables are managed by the shell
Any program that you start (including another shell) receives a copy of these variables which then may be read, modified, and passed on to their own child processes
Type env to see your environment variables and their values.
setenv• The following example sets the PATH variable to search for files in the /bin, /usr/bin, and /usr/sbin directories, in that order:
In c-shell: setenv PATH "/bin:/usr/bin:/usr/sbin“
setenv VARIABLE value
• To add the path /usr/ucb to the environment variable PATH:
csh% setenv PATH $PATH:/usr/ucb
• To avoid having to type this every time you login, put it in your .cshrc startup file
setenv (csh) vs. export (bash)
setenv PATH /home/fatalay/mpich2-install/bin:$PATH
export PATH=/usr/ccs/bin:$PATH
alias (csh) vs. alias (bash)
alias emacs /home/fatalay/emacs-23.4/src/emacs
alias emacs=/home/fatalay/emacs-23.4/src/emacs
Commands
Here are the most basic commands in Unix ls displays a list of files in the current directory cd directory change directories passwd change the password for the current
user cat textfile throws content of textfile on the screen pwd display present working directory exit or logout leave this session man command read man pages on command
Commands A command usually has three parts
Format: command [options(s)] arguments
The command ls
The options Almost always preceded by a hyphen ( - ) and no space between the hyphen and
the option ls -l
May group multiple options together, in any order ls -la ls -al
Many commands can be used without options ls
Some commands have no options The arguments
Specifications for the object(s) on which you want the command to take effect ls /homes
some options take arguments gcc -o myprog.out myprog.c gcc myprog.c -o myprog.out
Listing Files ls displays a list of files in the current working directory
-l long listing format -rw-rw-r-- 1 fatalay sjufacul 703 Jan 6 2014 test2.c
File type and permissions Number of hard links Owner name Group name Size in bytes Timestamp (by default, the modification time) file name
-a list all files/directories including hidden files (those that begin with .)
-R List the contents of all directories recursively
Changing Directories
cd Change directories
You can use relative path names or absolute pathnames cd CSC310 cd /home/fatalay/CSC310
. references the current directory cd .
.. references the parent director cd .. cd ../..
cd with no arguments will take you to your home directory from anywhere cd ~ will take you to home directory
cd / will take you to system root
Manual PagesThe man pages are the standard manual pages on all commands, system calls, etc that
exist on a Linux or Unix system. The pages are very structured. e.g. man man
The man pages are automatically formatted to show only one screen of information at a time
To go to the next page, press the space bar To quit, just hit q
The file system On a Unix system, almost everything is a file.
A directory is just a file containing names of other files
Programs, images, etc. are all just files
Input, output, and most other devices are considered to be files
Most files are called regular files Text files Data files Source code Executable code
Files types that are not regular files Directories (d) Special files (c or b) - files representing input and output devices, and others Links (l) - this file is linked to another file Named pipes (p) - one way to facilitate interprocess communication Sockets (s) - another way to facilitate interprocess communication, but over a network ls -l ls -l /dev ls -l /
/bin Common programs, shared by the system administrator and the users.
/lib Library files, includes files for all kinds of programs needed by the system and the users
/etc Most important system configuration files are in /etc, this directory contains data similar to those in the Control Panel in Windows
/boot The startup files and the kernel
/proc virtual file system containing information about system resources.
/sbin Programs for use by the system and the system administrator.
/usr Programs, libraries, documentation etc. for all user-related programs.
/var Storage for all variable files and temporary files created by users, such as log files, the mail queue, the print spooler area, space for temporary storage of files downloaded from the Internet, or to keep an image of a CD before burning it.
/dev Contains references to all the CPU peripheral hardware, which are represented as files with special properties.
/home Home directories of the common users.
Typical directories under root (/)
The path When we type in a command, say ls, we should have to type in its entire path name
(/bin/ls) in order for the system to find the utility and then execute it
So why are we able to just type in the command (in most cases)?
When we log on, certain environment variables are set
One is the PATH variable which defines the directories to search, and the order to search, for any command we enter
To see what your PATH variable is set to echo $PATH
My PATH is:/home/fatalay/bin/sun4:/fs/unsupported/ical/bin/sun4:.:/home/fatalay/bin:
/usr/local/bin:/bin:/usr/bin:/etc:/opt/local/bin:
If you enter a command, and it is not in one of the directories on your PATH, then the command will not be found You must then enter its entire path name to use it
File manipulation commandsNOTE: Most of these commands are not reversible, and they will
overwrite/delete existing files with no asking "Are you sure you want to do this?" SO BE CAREFUL
mkdir - makes a directory in the current directory mkdir my_new_directory
mv - moves a file from one name/place to another In the arguments the source comes first then the destination
mv from_file to_file There is no rename command, so you must use mv to rename a
file mv oldName newName
If you want to move a file from one directory to another, keeping the file names the same, then just list the "to directory" name as the destination
mv myFile /home/fatalay/newPlace
cp - copies a file from one name/place to another
source comes first then the destination cp from_file to_file
To copy a file from one directory to another keeping the same name, then just list the "to directory" as the destination cp myFile newDirectory
If you want to copy an entire directory cp -r this_dir that_dir
rm - removes a file rm myFile
rmdir - removes an EMPTY directory rmdir myDir
There will be no warning or second chance There is no "Recycle Bin" in Unix If you want to remove a directory that is not empty, use
the -r option to rm. This does a recursive remove rm -r myNotEmptyDir
find - finds files The find command is very powerful and complicated, but
to use it for simple file location, it is easy find starting_location -name filename
Note that . represents the current directory find . -name myFile.tmp
cat Concatenates files, displays on standard output cat file1 file2
One of the side effects of the cat command is that it displays the contents of the file on the screen cat myFile
-n Adds line numbers to the display cat -n myFile
I/O Redirection
Programs (including commands) typically read from stdin (typically the keyboard) and write to stdout (typically the screen).
stdin Standard input, usually the keyboard
stdout Standard output, usually your screen
stderr Standard error, usually your terminal
Output Redirection >
Redirects the output into the specified file If the file doesn't exist, it will be created If the file does exist, it will be overwritten (with no warning, so be careful)
ls -la > dirListing cat file1 file2 > file3
$ lsa.out myDir pt1.cpp test1.c test2.c$ ls -la >myDir$ cat myDirtotal 48drwxr-xr-x 2 fatalay sjufacul 512 Jan 26 17:53 .drwxr-xr-x 75 fatalay MACS 12800 Jan 26 17:51 ..-rwxr-xr-x 1 fatalay sjufacul 7780 Jan 15 12:39 a.out-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:55 myDir-rw-r--r-- 1 fatalay sjufacul 2010 Jan 15 12:37 pt1.cpp-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:52 test1.c-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:53 test2.c
>> Redirects the output into the specified file If the file doesn't exist, it will be created If the file does exist, the output will be appended to the contents of the file
| Pipe Links two or more commands together The output of the first command is "piped" to the second command as its
input $ ls -latotal 16drwxr-xr-x 4 fatalay fatalay 4096 Jan 27 13:47 .drwxr-xr-x 37 fatalay fatalay 4096 Jan 16 12:16 ..drwxrwxr-x 8 fatalay fatalay 4096 Jan 23 2013 Coursesdrwxrwxr-x 3 fatalay fatalay 4096 Jan 27 13:47 Research$ ls -la | wc -l5
Input Redirection <
When you want a file to be the input to a command (even if the command does not normally accept a file as an option)
$ sort12119Ctrl-d
112
19
$ sort < junk.data
Combine input/output redirection $ sort < junk.data > junk-sorted.data
Wildcards
A metacharacter is a character that has a special meaning instead of a literal meaning to a computer program, such as a shell interpreter
A wildcard character: Is a metacharacter since it has special meaning to the shell can be used to substitute for any other character or characters in
a string
Wildcards
* (Asterisk) Represents any number of characters including zero ls *.c
? (Question mark) Represents one, and only one, character ls ?.c will expand to only a.c
$ ls
a.c myDir pt1.cpp test1.c test2.c test3.c
$ ls *.c
a.c test1.c test2.c test3.c
$ ls ?.c
a.c
Wildcards [ ] (Square brackets)
Represents a list of characters, one of which must match ls *.[Tt]mp will expand to abc.tmp, abc.Tmp, .tmp, .Tmp
If characters are consecutive (according to their ASCII value), a range may be listed
ls myfile[1-5] will expand to myfile1, myfile2, myfile3, myfile4, and myfile5, but not to myfile or myfile0
ls myfile[a-d1-4] will expand to myfilea, myfileb, myfilec, myfiled, myfile1, myfile2, myfile3, and myfile4
Combining all three metacharacters ls *?myfile[A-Za-z0-9] will expand to:
Any number of characters, but at least one, before myfile followed by a single alphanumeric character (but not any punctuation marks)
! (Exclamation point) Negate a set of characters ls myfile[!0-9] will expand to all files that begin with myfile and ends with a single
character that isn't 0-9
Special Characters These are metacharacters that have special meaning to the shell, but are not wildcards since
they don't substitute for characters in a string
~ (Tilde) Expands to the home directory $ cd ~
$ pwd/home/fatalay$ cd ~tezel$ pwd/home/tezel$ cd ~/Documents$ pwd/home/fatalay/Documents
\ (Backslash) Escape character When placed before a metacharacter or special character, the literal value of the character is preserved Removes the special meaning of the character ls myfile\* expands to myfile*
$ (Dollar sign) Indicates that something is a variable echo $PATH echo $LOGNAME echo $SHELL
Special Characters ' ' (Single quotes)
Used to preserve the literal value of each character enclosed within the quotes $ echo "$SHELL"
/bin/csh$ echo '$SHELL'$SHELL
" " (Double quotes)
Preserves the literal value of all characters except: Dollar sign - retains its special meaning Back quotes - retain their special meaning Backslash - retains its meaning when followed by dollar sign, back quote, double
quote, backslash or newline
` ` (Back quotes) Command expansion Bash executes the command and replaces the command substitution with the
output of the command (with any newlines deleted) $ echo date
date$ echo `date`Fri Oct 10 09:34:34 EST 2008$ echo "Today, `date`, is someone's birthday"Today, Fri Oct 10 09:35:00 EDT 2008, is someone's birthday
Commands that process the standard output
sort - sorts lines of text files uniq - removes duplicate lines from a sorted file
head - prints first 10 lines of each file to standard output
tail - prints last 10 lines of each file to standard output diff - finds differences between two files more – browse or page through a text file
fgrep Given one or more files, print all lines in those files that contain a fixed
character string -i case insensitive
$ ls -latotal 50drwxr-xr-x 2 fatalay sjufacul 512 Jan 27 14:51 ./drwxr-xr-x 75 fatalay MACS 12800 Jan 27 15:56 ../-rw-r--r-- 1 fatalay sjufacul 0 Jan 27 14:51 a.c-rw-r--r-- 1 fatalay sjufacul 428 Jan 26 17:55 myDir-rw-r--r-- 1 fatalay sjufacul 2010 Jan 15 12:37 pt1.cpp-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:52 test1.c-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:53 test2.c-rw-r--r-- 1 fatalay sjufacul 0 Jan 27 14:41 test3.c$ ls -la | fgrep test-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:52 test1.c-rw-r--r-- 1 fatalay sjufacul 0 Jan 26 17:53 test2.c-rw-r--r-- 1 fatalay sjufacul 0 Jan 27 14:41 test3.c$ fgrep –i xray f1.dat f2.data … fn.dat | more
Read about: grep/egrep Given one or more files, print all lines in those files that contain a regular
expression pattern
You use wildcards for matching file name patterns, when you use directory or file commands
You use regular expressions to match text within files
Some Basics: (there are more rules than the ones mentioned here.) Surround a RE in single quotes
. (dot) Matches exactly one character (equivalent of the wildcard ?)
Repetition characters: ? Matches zero or one instance of the preceding character * Matches zero or more instances of the preceding character + Matches one or more instances of the preceding character
grep/egrep Lists and ranges
Works the same way as the wildcard brackets [a-z] means any character between a and z
Note that the - (hyphen) is not a metacharacter - it assumes special meaning only when it is between two characters in a list
The RE '[-v-x]' would mean a match on any one of the characters '-', 'v', 'w', or 'x' Example: Write a RE that would match a Social Security number
'[0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]'
$ lsa.c myDir pt1.cpp t.c test1.c test2.c test3.c$ ls | grep 't[a-z0-9]*.c'pt1.cppt.ctest1.ctest2.ctest3.c$ ls | grep 't[a-z]*.c't.c
File Permissions
Every file in Unix/Linux is owned by a user and a user group and has a set of permissions associated with each
A third category is others - anyone who is not the user or in the user's group
For regular files, there are three types of permissions Read - permission to only read the file Write - permission to modify (and delete) the file Execute - permission to run the file (only valid with scripts and
executable files)
File Permissions
For directories, again three types of permissions Read - permission to read the contents of the directory (but not
change to it or execute any of the files) Write - permission to write to the directory (create and delete
files and/or directories) Even if you don't have write permission for a file, if you have write
permission for the directory, you can delete the file Execute - permission to execute files in the directory, and
change to it (but not see what files are there)
File permissions$ ls -la
total 34
drwxr-xr-x 3 fatalay sjufacul 512 Jan 27 16:47 ./
drwxr-xr-x 75 fatalay MACS 12800 Jan 27 15:56 ../
drwxr-xr-x 2 fatalay sjufacul 512 Jan 27 16:47 TestDir/
-rw-r--r-- 1 fatalay sjufacul 0 Jan 27 14:51 a.c
col 1: 'd' means directory
'l' means link
'-' means file col 2-10: 'r' means read access 'w' means write access 'x' means
execute access (traversal access for directories) the first 'rwx' is user permissions the second 'rwx' is user group's permissions the third 'rwx' is others’ permissions
File Permissions
each 'rwx' group has associated with it a 3 bit octal number 000 nothing 001 just execute 010 just write 011 write and execute 100 just read 101 read/execute 110 read/write 111 read/write/execute
chmodrwx rwx rwx
111 000 000 //represented as 700 for chmod command
$ chmod 111 test.txt Changes permissions to 001 001 001
$ chmod 755 test.txt Changes permissions to 111 101 101
$ chmod 777 test.txt Changes permissions to 111 111 111
Whenever a file or directory is created, it is given default permissions A file rw-rw-rw- (666) A directory rwxrwxrwx (777)