Linux Shell Essentials
Useful Linux Commands!
Table of Contents:
Intro
Quick Command Descriptions
Command
Examples & Syntax
Intro:
This is a
mixture of the shell (command line tool) and some common linux system programs.
To use these commands you can either run Linux on your computer or use a
virtual machine with Linux installed on it. The shell that we are using is
called Bash (Bourne again SHell)
For Windows:
1. Cygwin (A bash shell for windows I
highly recommend)
2. WSL (Windows subsystem for linux)
For Mac:
1. Many of the commands will run from
the terminal in Mac (It might use slightly different syntax
You can
quickly navigate to a command by using the links here. To quickly navigate to a
command example click here, then click on the next link in the Quick command
description section which will take you there. Commands roughly ordered by
expected usefulness.
Directory structure |
pwd |
Ls Ls -lah dir |
cp |
mv |
mkdir |
cat |
echo |
>> |
> |
| |
awk / gawk |
sort |
Grep |
Wc |
Uniq |
Head |
Tail |
bash |
Ps/top |
file |
Lscpu |
Tab usage |
Less/vim/nano |
History |
df |
which |
tree |
File Permissions / chmod |
Assign variable |
Do while loop |
man |
|
|
|
The
directory structure in linux starts with a backslash this is called the root
directory. The root user in linux is the system administrator. To run commands
in linux as administrator you will often append su or sudo before the command
you want to run. This is important if you are running things on linux itself.
You can
often use the tab key to autocomplete commands. Commands are case sensitive,
this means that “Cat” is not the same as “cat” .
The user directories are stored in /home/user_name , another way of navigating to the current user's home directory is by using ~/
Many
configuration files will be stored in /etc
Quick command descriptions:
dir -
List directory (Windows)
mkdir -
Make Directory
cat - Read the contents of a file
and output to screen
echo - Literally echo, or output what
you put into it.
| - Pipe the output of a
prior command to the next command
awk - Programming language for text
processing and data extraction
sort - Sorts the contents of a file
uniq -
Compares each line in a file with the immediately following line to see if they
are different (If you may want to sort the contents first)
grep - Pattern matching tool, uses
Regular Expressions (a syntax for pattern finding)
wc - Word count, shows the
number of words, lines, and characters in a file
head - Read a file and output the
first 5 lines
tail - Read a file and output the
last 5 lines
bash - Used to run a BASH script
ps -
Process Status, shows a list of currently running processes on the operating
system (running programs)
top -
Interactive list of process list
kill - Kill, stops, ends a process
by the PID number
file - Tries to identify a file
type
lscpu -
Outputs the cpu description of your system
/proc/cpuinfo - On linux this likely
will do the same thing as lscpu
tab key -
Auto complete a command, or directory or filename (and some other stuff)
less - Outputs to screen and lets
you scroll through it. (Vim like key bindings)
vim - A text editor for the shell
/ command-line.
Tough for beginners, but powerful
and efficient. Widespread usage in Linux.
nano - A more friendly text editor for
the shell.
history - Prints the past commands that you
have run
df - Prints remaining amount
of disk space you have on a system
which - Used to find the location of a
command on a system
tree - Prints the directory tree
structure
chmod - Changes permissions on a file so
you can run it.
assign variable -
In bash you type the name equals and assign the value
e.g. my_variable = 1
to print a
value in a script you use the dollar sign before
e.g. echo $my_variable
wget - Downloads from the web, useful
tool because if the connection is interrupted
you can rerun the command and
continue from where it left off. (better than most web browsers)
for loop - Part of scripting in
BASH used to run some commands multiple times
do while loop -
Part of scripting in BASH
scp -
Secure Copy. A tool to securely transfer files via command-line/shell
ssh -
Secure Shell. A tool used to securely connect to a remote system and run
commands
screen - A tool that is used to prevent
commands that you are running in ssh from being interrupted if your connection
is interrupted.
Man - Manual, Get the instructions for a
command. Example ‘man screen’
Command Examples & Syntax
In the examples below $ indicates the line the command is
run on and the output is shown below.
# Will represent a comments and Descriptions. The colored
boxes are just for visual separation of the various commands to see them more
easily.
#Print working Directory
$ pwd
/cygdrive/c/users/UserName/desktop/ncl
#List files in folder
$ ls -lah
total 1.2M
drwx------+ 1 UserName UserName 0 Oct 22 17:17 .
drwx------+ 1 UserName None 0 Oct 22 14:29 ..
drwxr-xr-x+ 1 UserName UserName 0 Oct 22 16:48 bla
-rw-r--r--+ 1 UserName UserName 17 Oct 22 17:17 files
#Copy the
$ cp files_to_copy
files_copy_name
#Move file2 to the bla
folder
$ mv files2 ./bla
#Move file2 to the parent folder
$ mv files2 ../
$ mkdir -p hello
#Show contents of file in shell
$ cat hi_file2
blah blah blah
hi there Nawied and Howard
hi there rowan, cody, and sean
blah
Hello there
blah
blah
blah
# Print text to screen
$ echo "Hello there"
Hello there
# Append text from a command to a file
$ echo "Hello there" >>
hi_file
# Merge two files together
$ cat hi_file >> hi_file2
#Write a new file, or overwrite the
contents of an existing file
#E.G. delete a file and then make a new
one with the same name
$ echo "Hello there" >
hi_file
#Pipe the output of one command into another command
#Also shows a simple example of how to use awk
$ cat hi_file2 | awk '{print $1}'
blah
hi
hi
Hello
#Sort the contents of a file
$ sort hi_file2
hi there Nawied and Howard
hi there rowan, cody, and sean
blah
blah
blah
blah blah blah
Hello there
#Look for duplicate lines that are next to each other and
output only ones that don’t match
$ uniq hi_file2
blah blah blah
hi there Nawied and Howard
hi there rowan, cody, and sean
blah
Hello there
blah
#Look for all duplicates and output a list of unique lines
$ sort -u hi_file2
hi there Nawied and Howard
hi there rowan, cody, and sean
blah
blah blah blah
Hello there
# Look for text in a file. You will definitely want to
google more about how to use grep if you use any of its
# pattern matching capabilities.
$ cat hi_file2 | grep 'blah'
blah blah blah
blah
blah
blah
blah
#Look for a pattern in a file. -i makes the search
case-insensitive.
#Look for a
$ cat hi_file2 | grep -i "h"
blah blah blah
hi there Nawied and Howard
hi there rowan, cody, and sean
blah
Hello there
blah
blah
blah
#Show all the lines that don’t match
$ cat hi_file2 | grep -i -v
"b"
hi there Nawied and Howard
hi there rowan, cody, and sean
Hello there
#Shows words lines and character counts
$ wc hi_file2
8 20 109 hi_file2
#Shows number of lines
$ wc -l hi_file2
8 hi_file2
#Show first two lines in file
$ head -n 2 hi_file2
blah blah blah
hi there Nawied and Howard
#Show last two lines in file
$ tail -n 2 hi_file2
blah
blah
#Run a bash script called script.sh
$ bash script.sh
hi_file
hi_file2
#Show currently running processes
$ ps
PID PPID PGID
WINPID TTY UID
STIME COMMAND
620 1 620
21320 ? 197609 16:38:21 /usr/bin/mintty
621 620 621
23364 pty1 197609 16:38:21 /usr/bin/bash
908 621 908
27132 pty1 197609 20:21:57 /usr/bin/ps
#Show all currently running processes
with full information
#On linux you may need to run sudo ps -ef
$ ps -ef
UID PID PPID
TTY STIME COMMAND
UserName
911 621 pty1 20:22:41 /usr/bin/ps
UserName
620 1 ? 16:38:21 /usr/bin/mintty
UserName
621 620 pty1 16:38:21 /usr/bin/bash
#Kill a process, this shutdown my bash shell btw
$kill 621
#Identify a file type
$ file hi_file2
hi_file2: ASCII text
$ file Meta.jpg
Meta.jpg: JPEG image
data, JFIF standard 1.01, resolution (DPI), density 240x240, segment
length 16, Exif Standard: [TIFF image data, little-endian, direntries=8,
compression=JPEG
(old), manufacturer=Apple, model=Apple iPhone 5, xresolution=132,
yresolution=140,
resolutionunit=2, GPS-Data], baseline, precision 8, 1024x768, frames 3
#Show CPU information
$ cat /proc/cpuinfo
processor : 0
vendor_id : GenuineIntel
cpu family :
ABC
model
: 256
model name :
Intel(R) Core(TM) i7-9702 CPU @ 3.00GHz
stepping : 13
microcode : 0xCB
cpu MHz : 3000.000
cache size :
12287 KB
physical id :
0
siblings : 8
core id : 0
cpu cores : 4
# View and scroll up and down a large file
Less netflow.txt
#Use j to go up and k to go down (or arrow keys)
#Use q to quit
#Type /TCP and hit enter to search/highlight “TCP” in the
file
#Use nano editor
#At the bottom of the editor is a list of commands and the
shortcuts needed to use it
# ^X Exit means that you need to press ctrl + x to run the
Exit command
$ nano netflow.txt
#Basic Vim
$ vim netflow.txt
: to enter a command
:q to quit
:q! to force quit
:w! to write file
Esc to leave current mode
i to enter Insert/edit mode
In insert mode j goes down k goes up l goes right h goes
left
Shift + h goes to the beginning of a file
Shift + g goes to the bottom of a file
#Show a numbered list of recently run
commands
$ history
23 echo -?
24 ls
25 sha512sum urllib
26 ssh name@128.114.60.86
27 ssh username@128.114.60.86
28 exit
29 ls
#Show current amount of disk space
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
C:/cygwin64 231616508 154201976 77414532
67% /
#Locate a command on the system
$ which calc
/cygdrive/c/Windows/system32/calc
#Show the tree structure you are currently in
$ tree
.
├── bla
│ └── hello
├── files
├── files2
├── google.html
├── hi_file
├── hi_file2
├── index.html
├── Meta.jpg
├── netflow.txt
├── Nginx_log.txt
└── script.sh
2 directories, 10 files
#Give full permissions to a file
$ chmod 777 files
#Check permissions on that file
$ ls -lah
total 1.2M
drwx------+ 1 username username 0 Oct 22 20:38 .
drwx------+ 1 username None 0 Oct 22 14:29 ..
drwxr-xr-x+ 1 username username 0 Oct 22 19:57 bla
-rwxrwxrwx+ 1 username username 17 Oct 22 17:17 files
#Download a file from the internet
$ wget www.google.com
--2020-10-22 20:44:15-- http://www.google.com/
Resolving www.google.com
(www.google.com)... 172.217.6.68, 2607:f8b0:4005:80a::2004
Connecting to www.google.com
(www.google.com)|172.217.6.68|:80... connected.
HTTP request sent, awaiting response...
200 OK
Length: unspecified [text/html]
Saving to: ‘index.html.1’
index.html.1 [ <=> ]
13.41K --.-KB/s in 0.02s
2020-10-22 20:44:15 (704 KB/s) - ‘index.html.1’
saved [13727]
#Log into a remote computer with SSH (Normally Linux but can
work on other OS’s)
$ ssh admin@192.168.1.1
admin@192.168.1.1's password:
#Start a session so that if your ssh connection is
interrupted any currently running commands on the system
don’t stop after you disconnect and you can log back in to see the results. Run
this after you have successfully
ssh’ed into a computer
Screen -S i_named_this_session
#Reconnecting to a session after disconnecting, first ssh into
the computer then list currently running screens
Screen -ls
#Open a Screen
Screen -xS i_named_this_session
#Example of a for loop,
#For – means this is a loop and for every variable
“variable_name” in “something_else” do the following
#commands until you see it all done. $(cat files) gets a variable from the
command that is run inside the
#parentheses, in this case a list of filenames.
for filename in $(cat files)
do
cat $filename
done
#You can also run this as a single line
for filename in $(cat files); do cat
$filename; done
Comments
Post a Comment