Shell Cheatsheet

Assigning Variables

chapter=chap # no spaces
title="Greetings from Space" # with spaces
empty= # empty variable

Displaying Variables

echo $length # 80
echo length=$length # length=80
echo this is $title # this is Greetings from Space
echo ${chapter}5 # chap5 - to display with no space between variable and other parts of string

Special Variables

$? # exit status of the last command
$$ # process number of the current shell
$! # process number of the last background command
$0 # filename of the current script
$# # number of arguments
$n # arguments (n = 1, 2, ...)
$@ # equivalent of $1 $2 ... $n
$* # same as $@

Special Symbols and Escaping

  • Double quotes escape special characters, except for $, `, \c (c = any character)
  • Single quotes escapes $ as well
  • Backslash escapes single character
  • Combinations with backsllash:
    • \n – new line
    • \c – stay on the same line
    • \t – tab

echo * # equivalent to ls (print out current dir)
echo "*" # prints *
echo "$length" # prints 80 - $ is not escaped by double quotes
echo '$length' # prints $length
echo \$length # prints $length - escapes single character

Reverse quotes (`command`) – run command:

echo today is `date` # shows today's date
users=`who | wc -l`
echo $users # displays number of users logged in

Arguments Shift

# script called like this:
# thisscript hello world
a=$1; shift
echo $b $a # world hello


&& - second command will only run if the first one was successful
|| - second command runs only if first one fails

test -d ~/b && echo "b is directory" # will show "b is directory" only if b is directory
test -f ~/b || echo "b is not file, therefore b is directory" # equivalent to above

For and list

for i in here there everywhere # list
   echo $i
# will print 
# > here
# > there
# > everywhere

Lists are separated by ; or new line


if [condition1]
    echo "condition 1"
    echo "condition 2"

# or
if [condition1]; then
    echo "condition 1"
    echo "condition 2"
  • -a - and
  • -o - or
  • -gt - greater then
  • -lt - less then
  • -eq - equal
  • -ne - not equal
  • -ge - greater or equal
  • -le - less or equal

if [ $i -gt 7 -a $i -le 99] #$i between 7 and 99

While and Until

# While
while [condition]; do

# Until
until [condition]; do


case $i
  1) continue;;
  2) echo $i
  3) break;;

Arithmetic Operations (expr)

echo $n # prints 0+1 - treats as string by default

n=`expr $n+1`
echo $n # prints 1

n=`expr 2+3`
echo $n # prints 5
expr 5 "*" 6 # 30
Shell Cheatsheet

How to calculate and print number of files in each subfolder of the current folder on Linux

find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr

That is: find all subdirectories of current directory; for each subdirectory, find all files, and print file count and path. Finally it sorts the result to show folders with most files on top
Note that the first entry is the folder itself. Also this script omits soft links and does not count sub-folders as files.

For example:

[someone@somewhere]# cd /etc/X11/
[someone@somewhere]# find . -type d | while read DIR; do find $DIR -type f | printf "%-8s %s\n" $(wc -w) $DIR; done | sort -nr
10 .
7 ./xinit
3 ./xinit/xinitrc.d
0 ./xorg.conf.d
0 ./xinit/Xclients.d
0 ./fontpath.d
0 ./applnk

How to calculate and print number of files in each subfolder of the current folder on Linux

Installing Fedora to HDD using USB Flash Drive (via Windows)

Step 1: Creating Fedora Live USB

  1. Get a 1+ Gb USB Flash drive.
  2. Download Fedora LiveUSB Creator and install it
  3. Start Fedora LiveUSB Creator, and check Download Fedora list. If Fedora you want to install is in this list, select it and continue to step 6. Otherwise continue to the next step.
  4. Download Fedora
  5. In Fedora LiveUSB Creator, click Browse button in Use Existing Live CD frame and choose the .iso file you just downloaded
  6. If you are planning to use Fedora Live USB only once to install Fedora permanently on the hard drive, continue to the next step.
    If you want to boot from this USB regularly, it’s also a good idea to allocate some or all of the empty space on USB drive to Persistent Storage. Persistent Storage can be mounted as a drive in Fedora, while the rest of the USB drive will not be visible. This way you can keep documents and settings you need in Fedora on the same USB drive you are booting from.
  7. Click Create Live USB. Wait for progress to reach 100% and for status to scream Complete! Close FedoraUSB Creator.

Step 2: Booting up from Fedora Live USB and installing Fedora on HDD

  1. Insert USB stick into the system on which you want Fedora to be installed.
  2. Boot up the system and make sure it boots from USB (on Thinkpad T60 for instance, you need to click ThinkVantage button while it boots, choose Boot from Temporary Device and then choose USB HDD from the menu).
  3. When Fedora loads and reaches Login window, choose Automatic Login.
  4. Once you are in, you should see Live System User at the right top corner. If you want to run Linux from USB every time, you are done. Otherwise, continue to the next step.
  5. Click Install to Hard Drive icon on a desktop. Follow the wizard and proceed with further installation instructions.
  6. Reboot, when done, and choose to boot from HDD this time (remove USB stick).
  7. Review and install recommended updates
Installing Fedora to HDD using USB Flash Drive (via Windows)