Webjunk Introduction to Linux command
Introduction
Terminology
- Shell: program that interprets and acts on user input from the command line. Linux distros usually include a selection - bash, csh, tsh.
- Environment: set of defined variables passed to a program when it executes.
- Profile: set of environment variables and default configurations set up for a user at login
The key files that may be read during login include:
- /etc/profile
- /etc/passwd - sets your home directory and startup shell
- $HOME/.bashrc - assuming that your login is configured to use bash
- Useful online manuals are available at: Red Hat
- One page Linux manual
Linux - Basics
Shells
A shell is a program that is used by a user to communicate with the operating system kernel. The default shell for MSDOS is 'command.com'. The shell has two main jobs:
- Display a prompt
- Interpret and execute commands typed by the user
Linux has several popular shells including:
- Bourne shell (sh) - batch oriented
- cshell (csh) - C programmers (BSD)
- Korn shell (ksh) - best of both
Hierarchical file system
The files in a system form a hierarchy as in MSDOS, but unlike MSDOS there is, usually, only one file system on a computer running Linux. The single file system may be distributed across more than one physical disk drive. Within the file system each user:
- has his/her own main directory called 'HOME'
- can create sub-directories
- can set permissions that control access to any files or directories created
- can move around the system (where permissions allow)
Each file/directory
- has a name that is unique in its directory
- has a path name that describes its location in the system
The following symbols represent important directories and can be used in commands:
- / root directory - opposite to MSDOS!
- . current directory
- .. parent of the current directory
File names
Each Linux file has a filename that can have a maximum of 255 characters (depending on the file system in use). If the file is not in the current directory, the directory in which it is located is also required to give a full file specification. e.g.
hello.c
/u/fred/myprog.save
Many characters have a special meaning in Linux and should not be used in filenames. The safest characters are :
- Letters A - Z
- Digits 0 - 9
- Underscore _
- Period .
Filenames are case sensitive - FRED, Fred and fred are different names.
Logging in
The initial Linux prompt appears as:
login:
At the prompt type in your account name. Different systems require different actions to achieve the 'login' prompt. After typing your account name at the 'login' prompt, you will be asked to enter a password.
password:
Type in your password - note that it does not appear on the screen as you type it. If your password is entered correctly, a shell is loaded and a prompt displayed.
$_
Logging out
The command to leave the system depends on which shell you are using:
bash exit
C Shell logout
Any - EOF code ^D (Control + D)
^D is the Linux end of file character and will cause the shell to terminate, thus ending the login session.
If you are not sure which shell you are using type:
echo $SHELL
Command format
Linux commands lines, typed at the shell prompt, have the form:
command [Options] [Arguments]
Arguments are usually files and/or directories. Many commands require a list of one or more files to process. These files are mostly used only as input data and are not changed by the command. If a command requires input data and no files are specified, the command will read from the keyboard.
Options are preceded by a '-'. Most commands have several options which alter the output produced. Options can be listed separately or grouped together:
ls -l -t
is the same as
ls -lt
Directories
Commands for creating, removing and changing to directories are very similar to those used in MSDOS:
cd
change directory to $HOME - the user's login directory
cd path
make the specified directory current
mkdir path
create a new directory
rmdir path
remove a directory
Shell substitution
A Linux shell interprets * and ? as wild cards in the same way that the MSDOS shell 'command.com' does.
- fred? matches fred1 and fred2 but not fred
- fred* matches all filenames that begin with fred
The pattern [...] matches any characters placed inside the brackets
- fred[123567] matches fred1, fred2, fred3, fred5, fred6 and fred7
- fred[1-35-7] means the same thing
echo is a useful command that displays its arguments on the screen. It is especially valuable for experimenting with the meaning of wild card and other characters. Try:
echo hello world!
echo *
echo .*
Re-direction
Linux supports pipes and input-output re-direction using the same symbols as MSDOS.
- cmd < file - read input to cmd from file
- cmd > file - write output from cmd to file, overwrite an existing file
- cmd >> file - add data to the end of file , if it exists, or create new file
- cmd1 | cmd2 - pipe, execute both commands, output from cmd1 becomes input to cmd2
Common commands
Some of the more frequently used commands and options are introduced in this section. Refer to 'Commands reference' for more detailed descriptions.
List - ls
A list of files and sub-directories can be obtained using ls:
- ls
- ls /usr/bin
A more detailed listing can be obtained with the l option:
ls -l
Concatenate - cat
To look at the contents of a (text) file type 'cat filename' e.g.
cat .profile
In general cat takes a list of files as input and displays them one after another. If no filenames are provided, cat will read from the standard input, the keyboard, until Ctrl-D (end of file) is entered.
Page - pg
Larger files may disappear off the top of the screen before you can read them. Either use Ctrl-S and Ctrl-Q or use pg
pg .profile
The pg command can be used to scroll through the file. At the : prompt type h for help. pg is often used with pipes:
ls /usr/bin | pg
Copy - cp
To make a copy of a file 'cp oldfile newfile' e.g.
cp .profile prof
Move a file - mv
This command performs the same function as MSDOS ren: 'mv oldname newname' e.g.
mv prof new.prof
mv can also be used to move a file from one directory to another.
mkdir spare
mv new.prof spare
Remove a file - rm
This command performs the same functions as MSDOS del:
rm filename
Linux assumes that you know what you are doing. It may not ask for confirmation, even for *, unless the -i (interactive) option is used. Take care.
Exercise
Carry out the following operations and make a note of the commands used:
- Open a terminal window
- Use ls to create a file called junk
ls -l /usr/bin > junk
- Make 3 copies of junk called junk1, junk2, junk3
- Change the name of junk1 to commands
- Delete junk2 and junk3
- Examine the contents of commands with pg.
- How many options does pg provide at the : prompt?
- Create a sub-directory called temp and copy all of the files in your current directory into it.
- Make temp the current directory.
- Try:
ls .*
what is happening here?
- Delete all of the files in temp.
- Return to your home directory.
- Remove directory temp.
- What is the full path of the current directory?
- /usr/bin contains the Linux commands. How many commands begin with the letter 'c'?
Searching for files
find
The find command is a very useful tool for searching the file systems for files and carrying out actions on them. It takes the form
find <start from> <pattern type> <pattern> <action>
The default action is to just list matching files. For example:
find . -name *.txt
Searches the file system, starting from the current directory, for files ending in '.txt'. This example finds all
files in the system that have been modified in the last day:
find / -mtime 1
You can avoid a lot of error messages by su'ing to root before trying these.
Searching inside files
grep
The grep command is a very useful tool for patterns within text files. It takes the form
grep <pattern> <file list>
For example:
grep "fred" *.txt
Searches for the pattern fred in files ending in '.txt'. If the pattern expression is simple, as in this case, the quotes can be ommitted. This example finds all in the directory /etc and sub-directories that contain the pattern "eth0".
grep -R eth0 *
Exercise
Try the following:
- Open a terminal windows and su to root
- What is the command that searches the directory /etc/sysconfig and it's subdirectories for files containing the pattern eth0?
- What is the command that lists files in the directory /var/log that have been modified in the last 24 hours?
Basic command list
Command summary:
ls list names of all files in current directory
ls filenames list only the named files
ls -t list in time order, most recent first
ls -l list long; more information; also ls -lt
ls -R list recursive
ls -a list all files, including names starting with . (normally hidden)
cp file1 file2 copy file1 to file2, overwrite file2 if it exists
mv file1 file2 move file1 to file2 (rename), overwrite file2 if it exists
rm filenames remove named files (delete)
cat filenames print contents of named files (type)
wc filenames count lines, words and characters for each file
wc -l filenames count lines for each file
echo args print arguments on the\screen
sort filenames sort files alphabetically by line
tail filename print last 10 lines of file
tail -n filename print last n lines of file
tail +n filename print file starting from line n
cmp file1 file2 print location of first difference
diff file1 file2 print all differences
cd change directory to $HOME, login directory
cd path change to specified directory
cd .. change to directory above this one
pwd print working directory, current
mkdir dirname create a sub-directory
rmdir dirname remove a sub-directory
find ... Search for files
grep pattern files Search files for text patterns
Introduction
Terminology
- Shell: program that interprets and acts on user input from the command line. Linux distros usually include a selection - bash, csh, tsh.
- Environment: set of defined variables passed to a program when it executes.
- Profile: set of environment variables and default configurations set up for a user at login
The key files that may be read during login include:
- /etc/profile
- /etc/passwd - sets your home directory and startup shell
- $HOME/.bashrc - assuming that your login is configured to use bash
- Useful online manuals are available at: Red Hat
- One page Linux manual
Linux - Basics
Shells
A shell is a program that is used by a user to communicate with the operating system kernel. The default shell for MSDOS is 'command.com'. The shell has two main jobs:
- Display a prompt
- Interpret and execute commands typed by the user
Linux has several popular shells including:
- Bourne shell (sh) - batch oriented
- cshell (csh) - C programmers (BSD)
- Korn shell (ksh) - best of both
Hierarchical file system
The files in a system form a hierarchy as in MSDOS, but unlike MSDOS there is, usually, only one file system on a computer running Linux. The single file system may be distributed across more than one physical disk drive. Within the file system each user:
- has his/her own main directory called 'HOME'
- can create sub-directories
- can set permissions that control access to any files or directories created
- can move around the system (where permissions allow)
Each file/directory
- has a name that is unique in its directory
- has a path name that describes its location in the system
The following symbols represent important directories and can be used in commands:
- / root directory - opposite to MSDOS!
- . current directory
- .. parent of the current directory
File names
Each Linux file has a filename that can have a maximum of 255 characters (depending on the file system in use). If the file is not in the current directory, the directory in which it is located is also required to give a full file specification. e.g.
hello.c
/u/fred/myprog.save
Many characters have a special meaning in Linux and should not be used in filenames. The safest characters are :
- Letters A - Z
- Digits 0 - 9
- Underscore _
- Period .
Filenames are case sensitive - FRED, Fred and fred are different names.
Logging in
The initial Linux prompt appears as:
login:
At the prompt type in your account name. Different systems require different actions to achieve the 'login' prompt. After typing your account name at the 'login' prompt, you will be asked to enter a password.
password:
Type in your password - note that it does not appear on the screen as you type it. If your password is entered correctly, a shell is loaded and a prompt displayed.
$_
Logging out
The command to leave the system depends on which shell you are using:
| bash | exit |
| C Shell | logout |
| Any - EOF code | ^D (Control + D) |
^D is the Linux end of file character and will cause the shell to terminate, thus ending the login session.
If you are not sure which shell you are using type:
echo $SHELL
Command format
Linux commands lines, typed at the shell prompt, have the form:
command [Options] [Arguments]
Arguments are usually files and/or directories. Many commands require a list of one or more files to process. These files are mostly used only as input data and are not changed by the command. If a command requires input data and no files are specified, the command will read from the keyboard.
Options are preceded by a '-'. Most commands have several options which alter the output produced. Options can be listed separately or grouped together:
ls -l -tis the same as
ls -lt
Directories
Commands for creating, removing and changing to directories are very similar to those used in MSDOS:
cd | change directory to $HOME - the user's login directory |
cd path | make the specified directory current |
mkdir path | create a new directory |
rmdir path | remove a directory |
Shell substitution
A Linux shell interprets * and ? as wild cards in the same way that the MSDOS shell 'command.com' does.
- fred? matches fred1 and fred2 but not fred
- fred* matches all filenames that begin with fred
The pattern [...] matches any characters placed inside the brackets
- fred[123567] matches fred1, fred2, fred3, fred5, fred6 and fred7
- fred[1-35-7] means the same thing
echo is a useful command that displays its arguments on the screen. It is especially valuable for experimenting with the meaning of wild card and other characters. Try:
echo hello world!
echo *
echo .*
Re-direction
Linux supports pipes and input-output re-direction using the same symbols as MSDOS.
- cmd < file - read input to cmd from file
- cmd > file - write output from cmd to file, overwrite an existing file
- cmd >> file - add data to the end of file , if it exists, or create new file
- cmd1 | cmd2 - pipe, execute both commands, output from cmd1 becomes input to cmd2
Common commands
Some of the more frequently used commands and options are introduced in this section. Refer to 'Commands reference' for more detailed descriptions.
List - ls
A list of files and sub-directories can be obtained using ls:
- ls
- ls /usr/bin
A more detailed listing can be obtained with the l option:
ls -l
Concatenate - cat
To look at the contents of a (text) file type 'cat filename' e.g.
cat .profile
In general cat takes a list of files as input and displays them one after another. If no filenames are provided, cat will read from the standard input, the keyboard, until Ctrl-D (end of file) is entered.
Page - pg
Larger files may disappear off the top of the screen before you can read them. Either use Ctrl-S and Ctrl-Q or use pg
pg .profile
The pg command can be used to scroll through the file. At the : prompt type h for help. pg is often used with pipes:
ls /usr/bin | pg
Copy - cp
To make a copy of a file 'cp oldfile newfile' e.g.
cp .profile prof
Move a file - mv
This command performs the same function as MSDOS ren: 'mv oldname newname' e.g.
mv prof new.prof
mv can also be used to move a file from one directory to another.
mkdir spare
mv new.prof spare
Remove a file - rm
This command performs the same functions as MSDOS del:
rm filename
Linux assumes that you know what you are doing. It may not ask for confirmation, even for *, unless the -i (interactive) option is used. Take care.
Exercise
Carry out the following operations and make a note of the commands used:
- Open a terminal window
- Use ls to create a file called junk
ls -l /usr/bin > junk
- Make 3 copies of junk called junk1, junk2, junk3
- Change the name of junk1 to commands
- Delete junk2 and junk3
- Examine the contents of commands with pg.
- How many options does pg provide at the : prompt?
- Create a sub-directory called temp and copy all of the files in your current directory into it.
- Make temp the current directory.
- Try:
ls .*
what is happening here?- Delete all of the files in temp.
- Return to your home directory.
- Remove directory temp.
- What is the full path of the current directory?
- /usr/bin contains the Linux commands. How many commands begin with the letter 'c'?
Searching for files
find
The find command is a very useful tool for searching the file systems for files and carrying out actions on them. It takes the form
find <start from> <pattern type> <pattern> <action>The default action is to just list matching files. For example:
find . -name *.txtSearches the file system, starting from the current directory, for files ending in '.txt'. This example finds all files in the system that have been modified in the last day:
find / -mtime 1You can avoid a lot of error messages by su'ing to root before trying these.
Searching inside files
grep
The grep command is a very useful tool for patterns within text files. It takes the form
grep <pattern> <file list>For example:
grep "fred" *.txtSearches for the pattern fred in files ending in '.txt'. If the pattern expression is simple, as in this case, the quotes can be ommitted. This example finds all in the directory /etc and sub-directories that contain the pattern "eth0".
grep -R eth0 *
Exercise
- Open a terminal windows and su to root
- What is the command that searches the directory /etc/sysconfig and it's subdirectories for files containing the pattern eth0?
- What is the command that lists files in the directory /var/log that have been modified in the last 24 hours?
Basic command list
| ls | list names of all files in current directory |
| ls filenames | list only the named files |
| ls -t | list in time order, most recent first |
| ls -l | list long; more information; also ls -lt |
| ls -R | list recursive |
| ls -a | list all files, including names starting with . (normally hidden) |
| cp file1 file2 | copy file1 to file2, overwrite file2 if it exists |
| mv file1 file2 | move file1 to file2 (rename), overwrite file2 if it exists |
| rm filenames | remove named files (delete) |
| cat filenames | print contents of named files (type) |
| wc filenames | count lines, words and characters for each file |
| wc -l filenames | count lines for each file |
| echo args | print arguments on the\screen |
| sort filenames | sort files alphabetically by line |
| tail filename | print last 10 lines of file |
| tail -n filename | print last n lines of file |
| tail +n filename | print file starting from line n |
| cmp file1 file2 | print location of first difference |
| diff file1 file2 | print all differences |
| cd | change directory to $HOME, login directory |
| cd path | change to specified directory |
| cd .. | change to directory above this one |
| pwd | print working directory, current |
| mkdir dirname | create a sub-directory |
| rmdir dirname | remove a sub-directory |
| find ... | Search for files |
| grep pattern files | Search files for text patterns |
