Command Line Basics
Different Shells
When we speak of the command line, we are working in what is known as a shell. A shell is a program that passes typed commands to the computer’s operating system to execute. The following is a list of popular shells that you will encounter:
-
bash
-
dash
-
sh
-
ksh
-
zsh
-
csh
-
powershell
Current shell - echo $0
Default shell - echo $SHELL
Different *nixes
There are many different *nix versions and cousins. The flavors include *BSD, Linux, Unix, and others. Each version and implementation has some unique features to their shells. Some popular versions and distributions are:
-
Red Hat/CentOS
-
Fedora
-
Ubuntu
-
SUSE
-
Debian
-
Solaris
-
FreeBSD
-
MacOS/OS X
-
AIX
-
OpenBSD
File structure
-
File locations differ between distributions
-
Common directories
-
/
(Root of the file system) -
/root
(root user’s home directory)-
This is not the root(
/
) of the file system
-
-
/home/$USER
(Your files) -
/etc
(System configuration files) -
/media
(Mount points for removable systems, primarily linux flavors) -
/opt
(Typically commercial software packages installed in one place) -
/var
(Variable files that change regularly) -
/var/log
(Common default location for log files)
-
Command Line Interface (CLI) Package Installers
yum install <package>
apt-get install <package>
-
yum
/dnf
-
Red Hat
-
CentOS
-
Fedora
-
SUSE
-
-
apt-get
-
Debian
-
Ubuntu
-
(yum is in the process of being replaced by dnf)
Basic Navigation
-
CasE SensItiVity MatTERs
-
tab completion to complete command names, paths, or files
-
pwd
(Print working[current] directory) -
ls
(list files in directory) -
cd
(Change Directory)-
cd
orcd ~
changes from the current directory to the user’s home directory -
cd /path/to/directory
changes from the current directory to the defined directory -
cd ..
changes from the current directory to the directory one level up -
cd ../../path/to/directory
changes from the current directory to two levels up then down to the new directory -
cd -
changes from the current directory to the previous directory
-
This is not navigation, but you need to know this
-
^n
or^-n
reads as ctrl-n -
$ appended to the beginning of a word is a variable that can represent an option in that context
Command Line Switches
Commands are often followed by one or more switches at the command line.
When one -
is used, each character following it is an option.
When two '--' are used, the full string is considered the option.
ls -help
attempts to run the ls
command with the h,l,p options*
ls --help
prints the ls
help file to the screen
(*e
is invalid, but needed for the demo)
Important
|
In some fonts, a -- notation will appear as a single - when rendered.
|
Permissions
The following is the output of ls -lah
(long list,all files,human readable)
total 40 drwxr-xr-x 6 chuck staff 204B May 25 2016 . drwxr-xr-x 16 chuck staff 544B Oct 24 21:25 .. drwxr-xr-x 5 chuck staff 170B May 25 2016 fixtures -rw-r--r-- 1 chuck staff 6.0K May 25 2016 .coffee -rw-r--r-- 1 chuck staff 2.8K May 25 2016 image-factory-spec.coffee -rw-r--r-- 1 chuck staff 6.0K May 25 2016 main-spec.coffee -rw-r--r-- 1 chuck staff 7.5K May 25 2016 main-url-support-spec.coffee ---------- - ----- ----- ---- ------------ ---------------------------- | | | | | | | | | | | | | + File Name | | | | | | | | | | | +----------- Modification Time | | | | | | | | | +------------------- Size (in bytes) | | | | | | | +--------------------------- Group | | | | | +---------------------------------- Owner | | | +-------------------------------------- Links | +----------------------------------------------- File Permissions
(Diagram from linuxcommand.org)
drwxr-xr-x |
TUUUGGGOOO |
Field | Definition |
---|---|
T |
File Type
|
U |
User/Owner Permissions |
G |
Group Permissions |
O |
World/Other User’s permissions |
rwx
Character | Effect | Numerical Value |
---|---|---|
r |
read |
4 |
w |
write |
2 |
x |
execute |
1 |
chmod
changes the permissions of the file or directory
Command | Permissions |
---|---|
|
-rw-r—r-- |
|
-rwxrwxrw- |
|
drw-r-xr-- |
|
-rw—w—w- |
Useful Commands
* vi/vim * emacs * pico/nano | For editing files |
---|---|
cp |
for copying files |
mv |
for moving/renaming files |
rm |
for removing/deleting files or directories |
find |
for finding files |
grep |
for finding stuff in files |
tail/head |
for viewing end/beginning of files |
service/systemctl |
for starting/stoping/controlling services |
less |
show the contents of a file at the cli |
chmod |
for modifying file permissions |
tar |
for compressing and decompressing files |
sudo |
for running a command as a different user, typically root |
man |
for learning how to use commands |
ssh |
for connecting to other machines |
vi/vim commands
vim filename
- opens the designated file in normal mode
:help
- displays the vim help file
/texttosearch
- search for text in the document (case sensitive)
i
- insert mode to edit the file
esc
- return to command mode
:w
- write/save the file to disk
:q
- quit the editor if no changes were made or unsaved
:wq
- write/save the file and quit the editor
:q!
- quit the editor without saving the changes
cp command
cp filename /path/to/copy/to
cp filename newfilename
copies the file from the current location/name to the new location/name
cp -R /path/to/directory /path/to/new/directory
copies the the files and directories from the specified directrory to the new location
mv command
mv filename /path/to/new/location
mv filename newfilename
moves the file from the current location/name to the new location/name.
Note
|
mv also acts as the rename command.
|
mkdir command
mkdir directoryname
mkdir -p path/to/new/direcctory
mkdir -v
mkdir -m=744 directory
creates directories to store files and directories
rm command
rm filename
removes (deletes) the indicated file
rm -rf directoryname
removes (deletes) the indicated directory and all of its contents, including hidden files
Important
|
This is destructive. It cannot be undone. |
find command
find . -name 'filename.txt'
Searches for the filename.txt file in the current directory and sub directories
Change .
in the above command to a path to start at that path and search its sub directories
Other popular factors to search on include owner, time, type, size, file types
grep command
grep -iR pattern Documents/
Searches files for the phrase pattern
in a case insensitive(i) manner in and below(R) the local Documents
directory.
head/tail commands
head -n 15 filename.txt
head
displays the first lines of a file (10 by default, 15 in the above example)
tail -n 15 filename.txt
tail
displays the last lines of a file (10 by default, 15 in the above example)
tail -f filename.txt
The above use of the tail
command with the -f
flag continually rereads and displays the end of the file.
This is useful when monitoring a log file in real time, for example.
Use <ctrl>-c to stop reading the file.
service/systemctl commands
The service
and systemctl
commands control the status of services on the system.
The following examples are for controlling the Apache web server status.
service httpd {start,stop,restart,status}
systemctl {start,stop,restart,status} httpd
less command
less filename
Displays the contents of a file in the terminal window.
Use the up and down arrows to navigate the file.
Use the v
key to edit the file in the system’s default editor.
When you quit the editor, you return to the less
process.
Use a forward slash followed by text to search for to find particular types.
/texttosearchfor
tar command
tar zxvf filename.tar.gz
Extracts the contents of the tar.gz file to the current directory
tar zcvf newcompressedfile.tar.gz file1 file2 file3
Creates a new compressed file containing all the files indicated in the command
tar zcvf newcompressedfile.tar.gz /path/to/files
Creates a new compressed file containing all the files in the indicated directory
sudo command
sudo ls /var/log/messages
By default, runs a command as the root
user.
Adding -u <user>
will specify a different user to run the command as.
Pipe commands from one to another on a single line
The pipe |
symbol passes the output of a command to another command.
The following command will list the contents of the current directory in a long format.
The grep
command then filters and displays only the lines of text that contain the word filename
in a case insensitive manner.
ls -l|grep -i filename
-rw-rw-r--. 1 chuck chuck 0 May 28 20:01 FILENAME.ADOC
-rw-rw-r--. 1 chuck chuck 0 May 28 20:00 filename.csv
-rw-rw-r--. 1 chuck chuck 0 May 28 20:01 FileName.jpg
-rw-rw-r--. 1 chuck chuck 0 May 28 20:00 filename.txt
The following command will run the ps -ef
command and show the results that match the phrase ssh
on the screen
ps -ef|grep ssh
man command
man <command>
The man command displays the manual (help) page for the command indicated.
Perhaps the most useful and under-utilized command in this presentation.
ssh command
ssh someuser@webdevbox
ssh someuser@192.168.1.1
ssh myotherbox
The above commands open a connection from the current computer to a remote computer. You will be prompted for a password or utilize a matching ssh key for authentication. Once connected, all commands you execute are performed on the remote machine. If a username is specified, you are connected as that user. If no username is specified, your current username is assumed.
ssh someuser@example.com systemctl restart httpd
The above command will log into the remote machine as someuser and run the command specified. In this case, it is restarting the Apache webserver.
Other interesting commands and concepts to follow up on
These are commands and concepts that are useful to know but may fall into niche categories. Many of these will likely be useful at some point to this audience.
-
vimdiff
- for showing the differences between two text files -
netstat
/ss
- for showing open ports -
firewall-cmd
- for configuring firewall rules -
nmtui
- for configuring network manager from the command line -
git
- version control for files -
sed
- Stream line editor to change file contents without opening a full editor -
md5sum
sha1sum
(and others) creates a unique hash of a file to easily compare two or more files -
scp
securely copies files between computers -
!!
adds the last command to the current command line -
screen
ortmux
are terminal multiplexers, withtmux
being the more modern version -
selinux - kernel security model that has been known to interfere with programs running correctly
-
environmental variables - easily share configuration settings between applications and processes such as
$USER
,$HOME
,$EDITOR
,$BROWSER
-
regex - Regular Expressions are your friend (once you make friends with them)
Dangerous commands
These commands should not be used unless you really understand what you’re doing with them.
rm -rf /
- Will delete the entire file system
mv file /dev/null
- will move the file to a system device that will delete the file
:(){:|:&};:
- a fork bomb which creates a function and executes it until the system freezes
$COMMAND > /dev/sda
- overwrites data on the block device, in this case the main drive
mkfs.ext4 /dev/sda
- formats the block device using the ext4 filesystem, in this case the main drive.
mkfs.* may be a variety of filesystem types.
Note
|
This list is far from comprehensive.
It is provided to show that simple and innocent looking things can be very harmful.
Pay attention and understand the commands you copy and paste from the internet.
For more, search for dangerous linux commands in your favorite search engine
|
Bash system files
Bash is probably the most commonly used shell on *nix systems. Similar files will exist for other shells.
.bashrc
- configuration file for non-login shells
.bash_profile
- configuration for login shells
.bash_history
- history of the recent commands run in the bash shell
Troubleshooting
log files are your friend
Common Location — /var/log
Read recent system messages — tail /var/log/messages
Print Kernel messages — dmesg
Resources
-
vimtutor
to learn vim from the command line -
Vim Adventures https://vim-adventures.com (pay past level 3)
-
Vim Casts video tutorials by Drew Neil http://vimcasts.org
-
Command Line tutorial http://linuxcommand.org/lc3_learning_the_shell.php
-
Command Line Magic https://twitter.com/climagic
-
Julia Evans https://twitter.com/b0rk https://wizardzines.com
-
Explains the entered command based on the man page http://explainshell.com
-
Test your regex expressions http://regex101.com
-
Practice your knowledge of regex http://regexcrossword.com
Links to this presentation
html | gitlab |
---|---|