Basic Commands CSE493
Basic Commands CSE493
(basic commands)
OS Architecture
• Application Programs: (USER)
[student@desktopX -]$
CHAPTER 1
ACCESSING THE COMMAND LINE
The bash shell
[student@desktopX ~]$
• 1. username
• 2. workstation of which that user is created/working.This is
called the shell prompt.
• 3. ~ : home directory of the user, where user data is stored.
• 4. $ : shell is ready to accept normal user commands.
When a regular user starts, the default prompt ends with a $
character.
CHAPTER 1
ACCESSING THE COMMAND LINE
Shell basics
• Commands entered at the shell prompt have three basic
parts:
• Command to run
• Options to adjust the behavior of the command
• Arguments, which are typically targets of the command
CHAPTER 1
ACCESSING THE COMMAND LINE
4. window list: The bar that runs along the bottom of the screen.
The window list provides an easy way to access, minimize, and
restore all windows in the current workspace.
CHAPTER 1
Executing Commands Using the Bash Shell
Basic command syntax
• The GNU Bourne-Again Shell ( bash ) is a program that
interprets commands typed in by the user.
• Each string typed into the shell can have upto thre e parts: the
command , options (that begin with a - or --), and an
arguments.
16
Shell Intro
• A system program that allows a user to execute:
– shell functions (internal commands)
– other programs (external commands)
– shell scripts
• Linux/UNIX has a bunch of them, the most common are
– tcsh, an expanded version of csh (Bill Joy, Berkley, Sun)
– bash, one of the most popular and rich in functionality shells, an
expansion of sh (AT&T Bell Labs)
– ksh, Korn Shell
– zhs
– ...
17
Command Format
• Format: command name and 0 or more arguments:
% commandname [arg1] ... [argN]
• % sign means prompt here and hereafter.
• Arguments can be
– options (switches to the command to indicate a mode of
operation); usually prefixed with a hyphen (-) or two (--) in
GNU style
– non-options, or operands, basically the data to work with
(actual data, or a file name)
18
Shell I/O
• Shell is a “power-user” interface, so the user interacts with
the shell by typing in the commands.
• The shell interprets the commands, that may produce some
results, they go back to the user and the control is given back
to the user when a command completes.
• These system commands are often wrapped around a so-
called system calls, to ask the kernel to perform an operation
(usually privileged) on your behalf.
19
Command I/O
• Input to shell:
– Command name and arguments typed by the user
• Input to a command:
– Keyboard, file, or other commands
• Standard input: keyboard.
• Standard output: screen.
• These STDIN and STDOUT are often together referred to as a terminal.
• Both standard input and standard output can be redirected from/to a
file or other command.
• File redirection:
– < input
– > output
– >> output append
20
man
• Manual Pages
• The first command to remember
• Contains info about almost everything :-)
– other commands
– system calls
– c/library functions
– other utils, applications, configuration files
• To read about man itself type:
man man
21
date
Displays dates in various formats
• date
• man date
22
cal
24
sleep
• “Sleeping” is doing nothing for some time.
• Usually used for delays in shell scripts.
• % sleep 2 2 seconds pause
25
Command Grouping
• Semicolon: “;”
• Often grouping acts as if it were a single
command, so an output of different
commands can be redirected to a file:
• % (date; cal; date) > out.txt
26
alias
• Defined a new name for a command
• % alias dt=“date”
27
unalias
• Removes alias
• Requires an argument.
• % unalias dt
28
history
• Display a history of recently used commands
• % history
– all commands in the history
29
exit / logout
• Exit from your login session.
• % exit
• % logout
30
shutdown
• Causes system to shutdown or reboot cleanly.
• May require superuser privileges
• % shutdown -h now - stop
• % shutdown -r now - reboot
31
ls
• List directory contents
• Has whole bunch of options, see man ls for details.
• % ls
– all files except those starting with a “.”
• % ls -l
• % ls -a
32
cat
• Display and concatenate files.
• % cat
– Will read from STDIN and print to STDOT every line you enter.
• % cat file1 [file2] ...
– Will concatenate all files in one and print them to STDOUT
• % cat > filename
– Will take whatever you type from STDIN and will put it into the file
filename
• To exit cat or cat > filename type Ctrl+D to indicate
EOF (End of File).
33
Head/tail
Shows few entries of the data from a file
34
touch
• By touch a file you either create it if it did not
exists (with 0 length).
• Or you update it’s last modification and access
times.
• There are options to override the default
behavior.
• % touch file
• Stat filename
• % man touch
35
cp
36
mv
• Moves or renames files/directories.
• % mv <source> <destination>
– The <source> gets removed
• % mv file1 dir/
• % mv file1 file2
– rename
• % mv file1 file2 dir/
• % mv dir1 dir2
37
rm
• Removes file(s) and/or directories.
• % rm file1 [file2] ...
• % rm -r dir1 [dir2] ...
• % rm -r file1 dir1 dir2 file4 ...
38
script
• Writes a log (a typescript) of whatever happened in
the terminal to a file.
• % script [file]
• % script
– all log is saved into a file named typescript
• % script file
– all log is saved into a file named file
• To exit logging, type:
– % exit
39
mkdir
• Creates a directory.
• % mkdir newdir
• Often people make an alias of md for it.
40
cd
• Changes your current directory to a new one.
• % cd /some/other/dir
– Absolute path
• % cd subdir
– Assuming subdir is in the current directory.
• % cd
– Returns you to your home directory.
41
pwd
• Displays personal working directory, i.e. your current
directory.
• % pwd
rmdir
• Removes a directory.
• % rmdir dirname
• Equivalent:
– % rm -r dirname
42
ln
• Symbolic link or a “shortcut” in MS Windows
terminology.
• % ln –s <real-name> <fake-name>
43
chmod
• Changes file permissions
• Possible invocations
– % chmod 600 filename
– -rw------- 1 user group 2785 Feb 8 14:18 filename
(a bit not intuitive where 600 comes from)
– % chmod u+rw filename
(the same thing, more readable)
44
which
• Displays a path name of a command.
• Searches a path environmental variable for the
command and displays the absolute path.
• To find which tcsh and bash are actually in use,
type:
which tcsh
which bash
45
whereis
• Display all locations of a command (or some
other binary, man page, or a source file).
• Searchers all directories to find commands
that match whereis’ argument
• % whereis tcsh
46
locate and find commands
locate /bin/ls
48