Linux Commaned Line Survival Guide (For Beginners)
Linux Commaned Line Survival Guide (For Beginners)
net/linux-command-line-survival-guide
Linux Commaned
Line Survival Guide
( for beginners )
This is the Linux command line survival guide ( for beginners ).
• In this guide I’m going to cover the most important and most
common commands to get you up and running fast.
• If you learn the commands in this guide, you should have enough
info to get by and be competent.
• This guide is meant to be concise and practical.
• I’m including as much useful info as possible without making things
too complicated or advanced.
• Check the link in the description for copy and past examples of
anything you see in this video.
• There are a lot of other really useful commands and information that
I’m not including in this video because I’m trying to keep this simple
1 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
for beginners.
• For each command I’m showing the most common usage and
options.
• I’m assuming you’re using the BASH shell which is usually the
default on most distros. Some distros have other defaults and some
comanies use other shells as their standard ( ex: ksh ). Most of what
I show should work in most common shells.
First 3 Commands
NOTE - Anything that comes after a pound sign ‘#’ is a comment and is
not actually part of the command. It has no affect when run on the
commandline. Many of the command examples I use are documented this
way.
ls command:
pwd command:
cd command:
2 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
~ home dir
. current dir
.. parent dir
NOTE - Any file or dir starting with a ‘.’ is a hidden dir and isn’t visible by
default.
Command history:
3 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
A few shortcuts:
clear
cat test1.txt
copy Command
Copy files:
4 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
mv Command
Move files:
A soft link is also referred to as a symbolic link. A soft link is just a pointer
to a filename. The actual file name points to the data on disk. Removing a
soft link will not delete the original file. Removing the original file will not
delete the soft link but will result in a broken link that points to nothing.
Creating a hard link basically just creates another name for the same data
on disk. A file name is just a name that points to some data on a disk. It is
probably easier to think of it as an alternate name or alternate directory
entry and not just a link. A hard link is basically the same thing as the
original name for a file. Deleting the original file will not actually delete the
file if it still has hard links pointing to it. To delete a file all hard links to the
file need to be deleted.
Creating links:
5 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
touch Command
Change access and modification times of a file to current time. Also,
creates the file if it doesn’t exist.
touch test1.txt #
stat /etc/hosts
file /etc/hosts
stat /usr/bin/nslookup
file /usr/bin/nslookup
mkdir Command
Create dirs:
6 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
rm Command
Remove files:
rmdir Command
Remove empty directories:
rmdir dir1
rmdir dir1/dir2/dir3
rmdir dir1/a/b/sub1
find Commaned
Finding files is easy. Don’t worry too much about memorizing these right
away (although it wouldn’t hurt). Just save these and refer back to them
as the need arises.
7 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
find . -name *.txt -exec rm -i {} \; # confirm and delete every file found
find . -name *.sh -exec grep 'test' {} \; # search for string in matching files
locate Command
The locate command is an alternative to the find command. The locate
command is faster than find. It doesn’t actually search the filesystem.
Instead it searches a DB of files that is updated on a regularly scheduled
basis. It isn’t installed by default on many distros ( ex. Ubuntu ).
uptime Command
Show uptime and load factor:
uptime
8 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
uname Command
Show system info ( arch, kernel, OS, etc. ):
0 STDIN input
1 STDOUT output
2 STDERR error output
Redirection operators:
> overwrite
>> append
2>&1 redirect STDERR to STDOUT
ls asdf 2> test1.txt # redirect command output ( STDERR ) to a file and **OVE
ls asdf 2>> test1.txt # redirect command output ( STDERR ) to a file and **APP
ls asdf >> output.log 2>&1 # append STDOUT to file, also redirect STDERR
Background
You can run a command in the background by appending an ampersand
to it:
9 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
./server1 &
./process_data.sh &
Pipes
You can pipe output from one command to another like this:
sort Command
Sort lines with the sort command:
uniq Command
Use the uniq command to only show uniq lines. This works after sorting so
we first pipe to sort and then to uniq.
10 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
grep abc test.txt # search file for lines matching this string
grep -i abc test.txt # case insensitive
grep -v abc test.txt # exclude any matching line
grep -r abc # search for this text in every file recursively from current
wc Command
Word count and line count:
SED command
The stream editor - sed:
11 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
sed 's/abc/xyz/' test.txt # swap first occurance of ‘abc’ with ‘xyz’ in a file
sed 's/abc/xyz/g' test.txt # swap every occurance ( g for global )
sed 's/abc/xyz/gI' test.txt # same but case insensitive:
sed -i 's/abc/xyz/g' test.txt # changes the file in place
sed -E 's/a|b/x/g' test.tx # use extended regex
awk Command
Columns of text can be split and selected with awk. It will split on spaces
and tabs by default.
awk '{print $3, $5, $7}' test1.txt # select and print columns
awk '{print "Fields: "$3" -- "$5" : "$7}' test1.txt # control formatting
awk -F/ '{print $3, $5, $7}' test1.txt # change the field separator
awk -F: '{print $3, $5, $7}' test2.txt # change the field separator
awk -F, '{print $3, $5, $7}' test3.txt # change the field separator
ps -ef | awk '{print output $3, $5, $7}' # with piped input
cut Command
The linux command cut is used to split apart lines in a file.
ps -ef | cut -d ' ' -f 1 # split on space, print field 1 of piped input/output
12 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
Viewing Text
There are numerouse tools for viewing text.
echo command
Use the echo command to display strings and variables.
cat Command
Use the cat command to view or concatenate files:
Concatenating files:
head Command
Use the head command to view the begining of a file or piped output.
13 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
tail Command
Use the tail command to view the end of a file or piped output:
more Command
Display and page through text page at a time. Press space bar for next
page.
less Command
This tool is similar to ‘more’ but it has a lot more features. It doesn’t need
to read in an entire input file at one time so it starts faster with very large
files when compared to other tools like vi.
14 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
q quit
space scroll forward n lines ( window size )
enter scroll forward 1 line
arrow keys up / down / left / right
g go to first line
5g go to line 5
G End of file
Searching
During a search:
n next match
N previous match
Diff
Diff:
Nano
Nano is a simple terminal based text editor.
15 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
nano test1.txt
VI / VIM
VIM is a newer, improved version of the old VI editor that is common on
Linux and Unix systems.
• Not intuitive, hard / frustrating for beginners who don’t know what
they are doing.
• Standard - You can count on this editor to be present on almost any
Unix or Linux system.
• Really powerful once you know all the shortcuts.
• Very old systems have VI installed, newer systems have VIM. The vi
command is usually a shortcut to vim but not always.
vi test1.txt
vim test1.txt
VIM Modes:
VIM Commands:
:w write / save
:wq save and exit
:q exit when no changes were made
:q! exit without saving
i insert mode, so you can actually type
[esc] exit insert mode
a add a line ( basically also insert mode )
dd delete current line
yy yank - copy current line
p put - paste current line
0 beginning of line
$ end of line
[shift] - g jump to last line
:0 jump to beginning of file
16 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
VIM Search:
• The root user is basically the default admin user on Linux and Unix
systems. This user can do almost anything.
• Normal users will generally have much fewer permissions.
Changing passwords:
17 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
If you need to run commands as another user or as root there are two
main options. You can become that user with the su command or run
commands as that user with sudo.
su - Substitute User
sudo
The sudo command allows you to run commands as another user. This is
usually used to run commands with elevated priviliges or as a service
user.
• For this to work the current user will need to be setup in the sudoers
file.
• Allows for fine grained control over which commands can be run.
• Allows for accounting of who has used these permissions.
Adding Users
18 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
useradd:
adduser:
Permissions
In this example output snippet, column 1 is the permissions, column 2 is
the owner, column 3 is the group.
19 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
Permission values:
r read 4
w write 3
x execute 1
You can represent permissions with letters ( rwx ) or numbers. Letters are
easier but many people use numbers and you should be familiar with
them.
20 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
NOTE - To list files in a dir and view those files, you need both r and x
permissions on that dir.
Sticky bit: Permission bit normally set on directories. For any files in the
dir, only the file’s owner, the directory’s owner, or root can rename or
delete the file. Normally set on the /tmp dir
setuid - file can be executed with permissions of the owner setgid - file
can be executed with permissions of the group
a all
u user (owner)
g group
o other
= set
- remove
+ add
21 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
chmod 700 test1.txt # set rwx for user, nothing for group and other
chmod 444 test1.txt # set r for all
Chown - Ownership
22 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
du Command
Show disk space usage in a dir and search for large files:
du -sh * # show sizes for all files and dirs in current dir
du -sh * | sort -h # sort numerically by human readable format
free
free -h # human readable
ps Command
The ps command is used to show what processes are running on a
system.
kill Command
The kill command is generally used to kill processes. This command is
actually used to send different signals to processes but most of the time
those signals are used to terminate the process.
23 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
kill 150746 # kill process with this pid, gentle, allows cleanup
kill -9 150746 # force kill
kill -hup 150746 # can cause certain specific services to reload configs
kill -term 150750 # kill process with this pid, gentle, allows cleanup
kill -kill 150753 # force kill
• kill command
Top Command
• The top command has a huge number of options and sub
commands. We’re covering the most useful and practical details
here.
top
Top commands:
q quit
f manage fields
24 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
Htop Command
The htop command is a process viewer similar to top. It can be used with
a mouse.
Launch htop:
htop
htop commands:
arrows navigate
space tag or untag a process
F9, K “Kill” process: sends a signal which is selected in a menu
F10, q quit
F6, <,
Selects a field for sorting
>
I Invert the sort order
Search by command lines, highlight while typing, F3 for next,
F3, /
Shift-F3 for previous
F1, h,
Go to the help screen
?
Disks
Check:
Mount / unmount:
25 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
You may or may not need to install an extra package to support exfat. If
you have unknown file system errors this may help.
Packages:
Compressed Files
26 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
TAR files
Tar files are a common tool used to archive files and directories into a
single archive file.
GZip Files
GZip is the standard, most common compression tool ( not the best ).
Zip files
Zip files are also very common:
27 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
BZip
Bzip files are also common:
lzma and xz
lzma and xz are common and better than gzip and bzip:
7zip
7zip is also popular:
RAR
You might also find yourself working with rar files:
28 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
x="test this"
echo x
29 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
~/.bashrc
env
Your PATH
When you run a program the system searches for programs in the
directories that are in your path. You might want to add new dirs to your
path if those dirs contain scripts or binaries that you want to run.
Add a line like this to your bashrc if you want the changes to persist:
~/.bashrc
export PATH=$PATH:/opt/some_sw/bin
If a script or executable binary is on your path, the system will know where
to look for it and you will be able to run it just by typing the name of the
script or binary. If it is not on your path you will need to specify the
directory ( either relative or full path ). To run something in the current dir
you can use ‘./’ to specify the current dir.
30 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
Scripts
This is not a guide to scripting but we are going to show you how to create
extra, super, basic scripts. We are also going to show you how to run thos
scripts.
31 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
• For Debian / Ubuntu just use apt by default for most cases.
• For RHEL / CentOS / Fedora just use DNF when available.
dpkg
The dpkg tool is a package management tool for Debian / Ubuntu
systems. It is really used for managing package files and querying what is
installed on the system. It generally isn’t used for dependency
management, etc. so for most tasks you will just use apt instead.
apt
Apt is the package manager for Debian / Ubuntu systems. It works pretty
well.
32 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
RPM
This is generally used for working with individual package files or checking
what is on the system. Generally you should use YUM or DNF instead (
actually just DNF ).
YUM
Yum is obsolete but is very common. It has been replaced by DNF and
oftent times is just an alias for DNF. NOTE, when it is an alias for DNF,
update and upgrade will do the exact same thing.
33 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
DNF
DNF is the current, preferred, non-obsolete package manager for Redhat
based systems. It replaces YUM.
Pacman
Pacman is a nice package manager for Arch:
34 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
Emerge
Gentoo is a bit different. Here are some commands.
Zypper
Suse uses the Zypper package manager:
Network Commands
We’re going to cover some basic network commands. We’re assuming
you are using DHCP and that wifi is working. We will still show how to
configure a static IP temporarily. We’re not covering wifi or configuring
static, persistent connecitons in this guide. We’re going to cover that
separately in another guide.
• If you’re on a server you probably aren’t using wifi but you might
need a static IP.
• If you’re on a desktop you can usually setup wifi with a GUI tool. (
that is going to depend on your distro )
Most systems will either have the older package ( net-tools ) or the newer
package ( iproute2 ) or sometimes even both.
Newer commands
These commands are part of the newer package ( iproute2 ).
35 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
ip route add default via 192.168.0.1 dev eth0 # add default route
ip route add 192.168.3.0/24 via 192.168.0.1 # add route through gateway
ip route add 192.168.3.0/24 dev eth0 # add route through interface
More Commands
Telnet
36 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
Netcat
Netcat is an incredibly flexible tool. It does a ton of stuff. These are some
of the more common, useful things that it can do.
SSH
SCP
The scp command is used to transfer files to and from remote hosts using
an SSH connection.
37 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
DNS
38 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
NOTE - You might want to use sudo or login as root to use lsof or fuser to
make sure you can see everything.
List processes using network ports. Specify UDP or TCP and the port
number:
Important Files
These are some important files that you should be aware of:
39 of 40 10/21/2024, 7:11 PM
Firefox https://low-orbit.net/linux-command-line-survival-guide
40 of 40 10/21/2024, 7:11 PM