Below you will find some basic BASH Shell Commands grouped by category.
These will get you started and let you find your way around the system.
Basic System Commands
ls = list files and directories
ls –a = list all files (displays hidden files, those that start with a “.”)
ls –F = list files and their type (@ = link, / = subdirectory, * = executeable)
ls –l = long listing (d=directory, l=symbolically linked, b or c = device files,
n=named pipe, s=socket,-=all else). Also lists permissions.
ls –C = list in column format (ls –x = list files in row format)
ls –F = list files ordered by type
ls –R = (recursive) list files in specified directories and all subdirectories
ls –i = inode number
uname -a = display linux kernel version
file = display file type of file (example: file Mytext.txt)
locate = (or slocate) locates a program file. Example: locate inittab .
May need to rebuild database from time to time when using.
find = locate a file. Syntax = find + StartDirectory + -criteria + WhatToFind .
Examples: find /etc –name inittab = find all files named inittab
find /var –size +1024 = find all files greater than 1024
find /dev –type d = find all directories
which = like find, but works only with commands in the “PATH” variable.
cat = display contents of a text file (like the “type” command in DOS)
tac = list a file backwards (the reverse of c-a-t)
head = display first 10 lines of a file (or head -5 to display only 5 lines.)
tail = display last 10 lines of a file (or tail -8 to display only 8 lines.)
date = display date
cal = calendar
clear = clear screen
reset = reset terminal
finger = display information on system users
who = display users currently logged in
w = display currently logged in users and their tasks
whoami = display login name
id = display numbers associated with user account ( The User IDs (UIDs) and Group IDs (GIDs)
echo = display string that follows.
echo $SHELL = echo current shell.
man = display a manual page. (example: man whoami)
man –k + command = keyword search for man page (example: man –k whoami)
info = like man pages, display help info (example: info whoami)
help = display help for built in BASH commands (example: help echo)
exit = exit out of current shell
shutdown = shutdown Linux system. Examples: shutdown –h +15m = shutdown in 15 minutes.
shutdown –h now = shutdown immediately.
startx = start the XWindows GUI.
gdm = start GNOME login.
touch = create a blank, empty file
alias = make your own command substitute for a BASK command. Examples:
alias ls=”ls –la” = make all ls commands long listings that display hidden files.
alias dir=”ls” = make the variable “dir”, a DOS command mean “ls”.
mail = allows you to read your mail.
Login to different terminals = CTRL + ATL + F1, F2, F3, F4, F5, F6 (F7 = GUI).
You should have 7 ttys available by default.
F7 will always correspond the the X Server.
Disk Commands
pwd = print working directory (confirm directory currently in)
cd = change directory (just like DOS, except the “..” = recursion)
~ + username = current user’s home directory (example: cd ~cgermany)
mkdir = make a directory (like the md command in DOS)
mv = move command (basically like a copy and paste operation in your big fat sissified
Windows GUI. Sorry, just kidding. ) Syntax = mv + source + target .
mv = (rename). I listed this on a separate line, since it’s the equivalent of “rename”.
That’s right, the way you rename a file in Linux/Unix BASH is to “move” it,
but within the same directory.
Normal move example: mv MyHappyPlace.txt /root/cgermany/loony (different dir)
Rename move example: mv MyHappyPlace.txt MySuperFunHappyPlace.txt (same dir)
cp = copy file. Syntax = cp + source + target (-r = recursive)
rm = remove a file. Syntax = rm cgermanyFile.txt. Other options:
rm –f cgermanyFile.txt = “force”, that is, don’t nag Y/N with each delete.
rm –r cgermanyFile.txt = “recursive”, delete directory and any subdirectories
rm –rf cgermanyFile.txt = don’t ask, don’t tell, delete all subdirectories.
rmdir = removes a directory, but only if there are no files. To remove a directory and files
inside it, use the old rm command with the recursive option “-r”.
More and Less
more = display next full page of text
less = like more, but does more, allows use of cursor keys.
Note: “less” is “more”. Both less and more work well with ls and piping.
Example: ls –al | less = list, but pause for each page.
GREP
Grep is a most powerful UNIX search utility. Among other commands, it is very tasty.
It truly becomes versatile when combined with piping. It is an acronym that stands
for “Global Regular Expression Print”. Variations are:
grep = regular expression search
egrep = (or grep –E) extended regular expression search
fgrep = (or grep –F) no regular expression search (faster)
Examples of GREP:
1. grep “cgermany” MyDocument.txt = return lines that contain “cgermany”.
2. grep –v “cgermany” MyDocument.txt = return lines that DO NOT contain “cgermany”.
3. grep –i “cgermany” MyDocument.txt = case INSENSITIVE search.
4. grep “cg.rmany” MyDocument.txt = the “.” acts as a wild card.
5. grep “^cgermany” MyDocument.txt = only lines that start with “cgermany”.
tr = replace characters in a file sent via standard input. It works with redirection.
For example, to replace all uppercase “X” characters with lowercase “x” characters in a file:
tr x X </root/MyFile.txt . This does not change the file, just its output. To save the changed
contents into another file: tr x X </root/MyFile.txt >NewFILE .
sort = sorts contents by the first character on each line.
piping – takes output of one command and feed it as input to another command:
ls –l | less
mount | grep /dev/had
ls –l | more
cat MyFile.txt | tr x X | sort | pr –d | less
redirection = Take the output and redirect it to another command, a file, or the printer.
Example: cat /etc/inittab >StartupSETTINGS. You can also redirect two
streams to two different files. Example: cat /etc/inittab >StartupSETTINGS 2>Error.log .
tee = sends information to a file as well as Standard Output.
Example: cat MyFile.txt | tr x X | sort | pr –d | tee NewMyFile.txt | less
sed = looks for a string of text and replaces it with another string.
Syntax = sed s/StringToSearchFor/StringToReplaceWhatIsFound/
Example: cat MyText.txt | sed s/cgermany/C.Germany/
File and Directory Permissions
groups = view group membership of current user
chown = change ownership of file. Syntax = chown + NewOwner + NameOfFile/Dir
Example: chown cgermany AllBankAccountInfoForBillGates.txt = (single file)
chown –R cgermany AllDonaldTrumpsCreditCardNumbers = (directory and
recursively all files in it. Note that it is a CAPITAL R.)
chgrp = change group ownership of file. Example: chgrp GERMANYS BankInfo.txt
chmod = change permissions on a file.
USER GROUP OTHER (everyone)
rwx rwx rwx
Permissions: r = read w = write x = execute
|
Each permission has a numeric value:
r = 4
w = 2
x = 1
|
Combinations of these numeric values are what you pass to chmod as an argument to assign permissions:
read and write permission would be: 4 + 2, so 6.
read and execute permission would be: 4 + 1, so 5.
read, write and execute permission would be: 4 + 2 + 1, so 7.
Therefore:
chmod 777 = user full permission, group full permission, other full permission
chmod 677 = user read and write, no execute, group full, other full
You can also use substitutes with chmod. Example:
chmod u=rw,g=wx,o=rx cgermanyTextFile.txt = chmod 635
umask – specifies default permissions when a file is created. It specifies what is “taken away”.
0 = nothing taken away, 4 = read taken away, 2 = write taken away, 1 = execute taken away.
Example: umask 001 = take away execute permission from “other” by default,
leave “user” and “group” untouched.
Special Permissions
SUID = set user ID (can only be applied to compiled binary programs). Allows user executing program
to temporarily become a member of the group that owns the program.
SGID = set group ID. When set, a user creating a file is still its owner, but the group of
the directory she/he creates it in becomes the group of the file.
sticky bit = user can add files to directory, but may only delete files that user owns.
Special permissions work numerically like general permissions, and are prefixed to
the beginning of the argument passed to chmod:
|
SUID |
SGID |
STICKY
BIT |
|
4 |
2 |
1 |
Therefore: chmod 7777 = set all special permissions and full general permissions.
chmod 1777 = set sticky bit and grant full general permissions
Users and Groups
useradd = add user. Examples:
1. useradd cgermany
2. useradd –g root –G accountants –p password cgermany
(-g = primary group, -G = other groups, -p = password)
3. useradd –u 777 cgermany = create user cgermany with a user ID of 777.
usermod = modify user. Examples:
1. usermod –l cgermany cfrance = change login name from cfrance to cgermany.
2. usermod –G production cgermany = add cgermany to production group.
3. usermod –L cgermany = lock the user account cgermany
4. usermod –U cgermany = unlock the user account cgermany
userdel = delete a user ( Example: userdel cgermany )
groupadd = add group ( Example: groupadd shipping )
groupmod = modify group
groupdel = delete group
pwconv – encrypt passwords ( Example: pwconv )
pwunconv = unencrypt passwords ( Example: pwunconv )
newgrp = changes primary group temporarily ( Example: newgrp root )
chage = change password expiration Example:
chage –m 4 –M 30 –W 5 cgermany = (-m=min, -M=max, -W=warn), so cgermany
must wait 4 days to change passwords, can only have the same password a
maximum of 30 days, and will be warned for 5 days prior to expiration)
gpasswd = change group password
passwd = change user password Examples:
1. passwd cgermany = change password for cgermany
2. passwd –u cgermany = unlock cgermany
3. password –l cgermany = lock cgermany
Mounting and Managing File Systems
mkfs =
format a disk with a particular filesystem.
Examples:
1. mkfs –t msdos /dev/fd0 = formats floppy as DOS
2. mkfs –t ext2 /dev/fd0 = format floppy as UNIX
mount = used to display all mounted drives
mount –t ext2 /dev/fd0 /mnt/floppy = mounts a floppy drive to the directory
/mnt/floppy. Then, you can used “cd
/mnt/floppy” and “ls” to view its contents.
mount –r –t iso9660 /dev/cdrom /mnt/cdrom = mounts a cdrom to the directory /mnt/cdrom. The “-r” switch
must be used to specify read-only, since most are not writeable.
The “-t” switch specifies the file system. Then, you can used
“cd /mnt/cdrom” and “ls” to view its contents.
mount –t ext3 /dev/hda3 /hold = mounts an ext3 partition on the 1st drive to /hold.
mount –t vfat /dev/hdb1 /hold = mounts FAT32 file system to /hold
mount –t ntfs /dev/hdb2 /hold = mountd NTFS files system to /hold, if and only IF you have installed the RPM for
reading NTFS files for Fedora Core 4 that matches your kernel version (Note:
You can use the uname –a to find out).
mount / -o remount,rw = remount file systems after making changes to /etc/fstab.
Mounting Network Shares
1. mount –t nfs RemotePCname:/ExportedDirectoryName /LocalMountPointDirectory
2. mount –t smbfs RemotePCname:/ExportedDirectoryName /LocalMountPointDirectory
3. mount –t cifs RemotePCname:/ExportedDirectoryName /LocalMountPointDirectory
umount /mnt/floppy = unmounts the floppy drive. You must unmount a device before you remove it.
Special Files: /etc/fstab = Filesystem table file. Holds/contains all drives to be mounted
automatically at boot.
The way Linux/Unix organizes the files system and identifies hard drives:
|
1.
Label the Physical Drive |
2.
Add the partition number: |
|
hda = master on IDE port 0 |
hda1 = 1st partition on hard drive 1 |
|
hdb = slave on IDE port 0 |
hdc5 = 5th partition on hard drive 3 |
|
hdc = master on IDE port 1 |
|
|
hdd = slave on IDE port 1 |
|
fdisk = format utility for Unix/Linux. Options:
m = menu
p = display partition information
n = create a new partition (specify starting and ending cylinders or start and
+1000M, +2000M etc. to automatically calculate size.)
d = delete partition
w = save/write partition changes to disk and exit
q = quit without saving any changes
l = list the partitions. Great for mounting NTFS and VFAT systems to what you have.
Note: I have noticed that this utility does not report the starting cylinder accurately on my Fedora Core 2
and Fedora Core 4 operating systems. Therefore, when using the “n” option to specify creating a
new partition, do not go by the starting cylinder it presents. Instead, go with 1 + whatever the last
cylinder number is when you do a “p” to display the partition table information from the main menu.
After creating and saving the partition, it can then be formatted with the command: mkfs –t ext2
/dev/hda4 . So the partition can be used, it must then be mounted using mount –t ext2 /dev/hda4
/DestinationDirectory . To cause the partition to be automatically mounted each time the system is
booted, it can be added to the /etc/fstab file.
df = lists free space on mounted partition.
Switches: df -h = break into mega, giga, etc.
du = lists size of a directory and its contents in kilobytes. Can be used with piping:
du /root/HoldMe = display size and contents of “HoldMe”
du –s /root/HoldMe = display SUMMARY of size and contents of “HoldMe”
du –h /root/HoldMe = display size and contents of “HoldMe” in mega/giga
tune2fs = converts ext2 files system to ext3 (much like Microsoft’s “convert” utility converts FAT32 to NTFS).
Syntax = tune2fs –j /dev/hda4 .
mke2fs = like tune2fs, converts ext2 to ext3. Syntax = mke2fs –j /dev/hda4 .
dumpe2fs = display total number of inodes and free in odes remaining on file system. An ext2/ext3 file system
has a fixed number of inodes, and many small files can eventually exhaust their supply. Syntax =
dumpe2fs –h /dev/hda4 will show the used and free inodes on a partition.
Repairing the File System
fsck = file system check, used to check, diagnose and repair the file system. It can check many different types
of file systems. It is IMPORTANT to note that file system being checked or repaired must be unmounted
BEFORE running fsck. This is fine if checking another partition other than the one with the operating
system, but if you must repair the operating system partition, you must reboot the Linux/Unix server
and go into run level 1 (init 1), so that you can unmount the OS partition. Examples:
fsck –t ext2 /dev/hda4 = simple check/scan of file system on hard drive
fsck –f –t ext2 /dev/hda4 = FORCES complete check/scan of file system on hard drive
fsck –f –t ext2 /dev/fd0 = FORCES complete check/scan of file system on floppy
e2fsck = like fsck, but has more options and can check file system more thoroughly. Also, it can only check
ext2 and ext3 file systems. Syntax = e2fsck –c /dev/fd0 . The –c option causes the program to check
for bad blocks and add them to the bad blocks table.
System Version and Configuration
uname -r = display the Linux kernel version you are using. You may also check the LILO or GRUB
configuration files in /etc/lilo.conf and /boot/grub/grub.conf, respectively.
setserial = sets IRQ, I/) and speed of serial devices.
system-config-display = configure the system display
system-config-mouse = configure the system mouse (also “mouseconfig”)
set = by itself, displays environment variables.
env = display list of all exported variables.
export = Variables by default are only accessible to their local shell.
The export command makes them available to all shells.
Syntax = export TheVariable .
Shell MetaCharacters
$ = shell variable (example: echo My command shell is $SHELL)
~ = home directory variable
& = background command execution
; = command termination, used with multiple commands to separate them
< << > >> = input/output redirection
| = command piping
* ? [] = wildcards
‘”\ = Metacharacter quotes
() { } = command grouping
The vi Editor
Like the “edit” utility in DOS, the vi editor is a text editor. It allows you to view, save, search,
correct and modify text. This particular tool is a favorite among UNIX old-timers, who consider
it to be tasty, tasty. Old school is not always obsolete. It is a command line staple, if Linux
were beans the vi editor could be the rice. Or not. Where does the name come from? Don’t
remember. Don’t care. Look it up on Google. Do something. Do I have to do everything? :-)
Anyway, Linux’s version of the vi editor is called “vim” for “vi – improved”. Naturally. Even still,
you just type the old “vi” to use it. There is a tutorial program you can run by typing “vimtutor”.
To create a new text file, just type “vi” at the shell prompt. To edit an existing text file, type
“vi + FileName”. Inside the vi editor, commands are as follows:
esc = change to command mode when in insert mode
i = change to insert mode to enter text (before character)
A = change to insert mode to enter text (after character)
SHIFT-I = change to insert mode to enter text (beginning of line)
SHIFT-A = change to insert mode to enter text (end of line)
SHIFT-O= change to insert mode to enter text (create new line above)
O - change to insert mode to enter text (create new line under)
Links
Links can be sausages, links can be hyper, links can be weakest, links
can be missing, and links can be up - get it?
sausage link + hyper link + weakest link + missing link + up link
That was a feeble attempt at humor. Anyway, to make a comparison to
Microsoft, in Unix/Linux a link acts as a shortcut. To compare it to C++
or C, a link is a pointer to another object. There are two basic kinds:
hard links = direct copies of other files, all same size, linked
symbolic links = (soft links) shortcut or “pointer” to a file
Hard linked files share the same inode, that is, they have the same size, permissions,
date, ownership, modification, etc. When one hard-linked file is modified, all the rest
are as well. The file system keeps an inode table consisting of inodes (information nodes).
Each inode contains a unique ID for each file and directory on the file system. The
“superblock” contains the number of inodes and data blocks, how much data a data block
stores in Kb, and general file system information. Soft-linked files do not share the same inode.
Hard links use the plain old “ln” command.
Syntax = ln + FileToLink + NameOfLink
Example: ln MyHappyPlace.txt PointerToMyHappyPlace
Soft links use “ln –s”. The “-s” switch makes it soft.
Syntax = ln –s + FileToLink + NameOfLink
Example: ln –s MyHappyPlace.txt PointerToMyHappyPlace
|