Dev To Awwsmm 101 Bash Commands and Tips For Beginners To Experts 30je
Dev To Awwsmm 101 Bash Commands and Tips For Beginners To Experts 30je
Until about a year ago, I worked almost exclusively within the macOS and
Ubuntu operating systems. On both of those OSes, bash is my default shell.
I've acquired a general understanding of how bash works over the past six
or seven years and would like to give an overview of some of the more
common / useful commands for those just getting started. If you think you
know everything there is to know about bash , take a look below anyway --
I've included some tips and reminders of flags you may have forgotten
about, which could make your work a bit easier.
; / && / &
Getting Help
-h
man
nano / nedit
mkdir / rm / rmdir
Command History
Directory Trees, Disk Usage, and Processes
mkdir –p / tree
df / du / ps
Miscellaneous
passwd / logout / exit
clear / *
Intermediate
Disk, Memory, and Processor Usage
ncdu
top / htop
locate / find
Downloading Things
ping / wget / curl
0 / 1 / 2 / tee
Advanced
Superuser
sudo / su
!!
File Permissions
File Permissions
chmod / chown
cut / sed
Pattern Matching
grep
awk
rsync
Long-Running Processes
yes / nohup / ps / kill
Miscellaneous
pushd / popd
xdg-open
xargs
The Basics
First Commands, Navigating the
Filesystem
Modern filesystems have directory (folder) trees, where a directory is either
a root directory (with no parent directory) or is a subdirectory (contained
within a single other directory, which we call its "parent"). Traversing
backwards through the file tree (from child directory to parent directory) will
always get you to the root directory. Some filesystems have multiple root
directories (like Windows' drives: C:\ , A:\ , etc.), but Unix and Unix-like
systems only have a single root directory called \ .
pwd / ls / cd
When working within a filesystem, the user is always working within some
directory, which we call the current directory or the working directory. Print
the user's working directory with pwd :
[ andrew@pc01 ~ ]$ pwd
/home/andrew
List the contents of this directory (files and/or child directories, etc.) with
ls :
[ andrew@pc01 ~ ]$ ls
Git TEST jdoc test test.file
Bonus:
[ andrew@pc01 ~ ]$ cd TEST/
[ andrew@pc01 TEST ]$ cd A
[ andrew@pc01 A ]$ pwd
/home/andrew/TEST/A
[ andrew@pc01 A ]$ cd ..
[ andrew@pc01 ~ ]$ pwd
/home/andrew
Bonus:
; / && / &
The things we type into the command line are called commands, and they
always execute some machine code stored somewhere on your computer.
Sometimes this machine code is a built-in Linux command, sometimes it's
an app, sometimes it's some code that you wrote yourself. Occasionally,
we'll want to run one command right after another. To do that, we can use
the ; (semicolon):
# the first command passes now, so the following commands are run
[ andrew@pc01 ~ ]$ cd Git/Parser/ && pwd && ls && cd
/home/andrew/Git/Parser
README.md doc.sh pom.xml resource run.sh shell.sh source src target
...but with ; , the second command will run even if the first one fails:
& looks similar to && but actually fulfils a completely different function.
Normally, when you execute a long-running command, the command line
will wait for that command to finish before it allows you to enter another
one. Putting & after a command prevents this from happening, and lets you
execute a new command while an older one is still going:
[ andrew@pc01 ~ ]$ jobs
[1]+ Running cd Git/Parser/ && mvn package &
Getting Help
-h
Type -h or --help after almost any command to bring up a help menu for
that command:
[ andrew@pc01 ~ ]$ du --help
Usage: du [OPTION]... [FILE]...
or: du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.
Mandatory arguments to long options are mandatory for short options too.
-0, --null end each output line with NUL, not newline
-a, --all write counts for all files, not just directories
--apparent-size print apparent sizes, rather than disk usage; although
the apparent size is usually smaller, it may be
larger due to holes in ('sparse') files, internal
fragmentation, indirect blocks, and the like
-B, --block-size=SIZE scale sizes by SIZE before printing them; e.g.,
'-BM' prints sizes in units of 1,048,576 bytes;
see SIZE format below
...
man
Type man before almost any command to bring up a manual for that
command (quit man with q ):
NAME
ls - list directory contents
SYNOPSIS
ls [OPTION]... [FILE]...
DESCRIPTION
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is speci-
fied.
head outputs the first few lines of a file. The -n flag specifies the number
of lines to show (the default is 10):
tail outputs the last few lines of a file. You can get the last n lines (like
above), or you can get the end of the file beginning from the N -th line with
tail -n +N :
# prints the end of the file, beginning with the 4th line
[ andrew@pc01 ~ ]$ tail -n +4 c
exactly
six
lines
cat concatenates a list of files and sends them to the standard output stream
(usually the terminal). cat can be used with just a single file, or multiple
files, and is often used to quickly view them. (Be warned: if you use cat in
this way, you may be accused of a Useless Use of Cat (UUOC), but it's not
that big of a deal, so don't worry too much about it.)
[ andrew@pc01 ~ ]$ cat a
file a
[ andrew@pc01 ~ ]$ cat a b
file a
file b
less is another tool for quickly viewing a file -- it opens up a vim -like
read-only window. (Yes, there is a command called more , but less --
unintuitively -- offers a superset of the functionality of more and is
recommended over it.) Learn more (or less?) about less and more at their
man pages.
nano / nedit
All modern editors offer basic conveniences like search and replace, syntax
highlighting, and so on. vi(m) and emacs have more features than nano
and nedit , but they have a much steeper learning curve. Try a few different
editors out and find one that works for you!
touch
touch was created to modify file timestamps, but it can also be used to
quickly create an empty file. You can create a new file by opening it with a
text editor, like nano :
[ andrew@pc01 ex ]$ ls
[ andrew@pc01 ex ]$ nano a
...editing file...
[ andrew@pc01 ex ]$ ls
a
[ andrew@pc01 ex ]$ nano a
Double Bonus:
mkdir / rm / rmdir
You can remove any file with rm -- but be careful, this is non-recoverable!
[ andrew@pc01 ex ]$ rm a && ls
b c
You can add an "are you sure?" prompt with the -i flag:
[ andrew@pc01 ex ]$ rm -i b
rm: remove regular empty file 'b'? y
...but you can remove a directory -- and all of its contents -- with rm -rf
( -r = recursive, -f = force):
mv / cp / ln
mv moves / renames a file. You can mv a file to a new directory and keep
the same file name or mv a file to a "new file" (rename it):
cp copies a file:
[ andrew@pc01 ex ]$ cp e e2 && ls
b c d e e2
ln creates a hard link to a file:
[ andrew@pc01 ex ]$ ln -s b g && ls
b c d e e2 f g
Hard links reference the same actual bytes in memory which contain a file,
while soft links refer to the original file name, which itself points to those
bytes. You can read more about soft vs. hard links here.
Command History
bash has two big features to help you complete and re-run commands, the
first is tab completion. Simply type the first part of a command, hit the <tab>
key, and let the terminal guess what you're trying to do:
bash keeps a short history of the commands you've typed previously and
lets you search through those commands by typing ^r (Ctrl+r):
[ andrew@pc01 dir ]
(reverse-i-search)`':
mkdir –p / tree
mkdir , by default, only makes a single directory. This means that if, for
instance, directory d/e doesn't exist, then d/e/f can't be made with
mkdir by itself:
But if we pass the -p flag to mkdir , it will make all directories in the path
if they don't already exist:
3 directories, 2 files
You can hide empty directories in tree 's output with --prune . Note that
this also removes "recursively empty" directories, or directories which aren't
empty per se, but which contain only other empty directories, or other
recursively empty directories:
df / du / ps
df is used to show how much space is taken up by files for the disks or
your system (hard drives, etc.).
[ andrew@pc01 ex ]$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 126G 0 126G 0% /dev
tmpfs 26G 2.0G 24G 8% /run