A bash quick reference.

If you want to talk about your personal paperweight, you've found the right place!

Moderator: Moderators

Post Reply
Ric
Simple Hen
Posts: 69
Joined: Sun Jun 03, 2007 2:25

A bash quick reference.

Post by Ric » Tue Jun 09, 2009 1:01

A friend wanted a brief, one page quick reference of Bash to help him fix his (frequently) broken system, or at least get him back to a working desktop. He's running Ubuntu, but the basics should be the same. Now its turning into a bit of a project for me....
Before I give it to him, does anyone see any mistakes, or something obvious missing?

Bash quick reference 0.1 An emergency reference for linux.
Most commands work thus: (command) -(option codes) (file name) or /(directory name).
For documentation on each command, use "info (command)", "man (command), or "(command) --help".
Eg:"info ls", or "mv --help". Adding the pipe "|less" can make large documentation more
managable. Eg: "ps --help | less". To exit documentation, press "q", or "(esc)q".

su Switch User. By default this gives administrative permissions to your terminal and applications started
from it. Root directory becomes the root of the partition, as opposed to a normal user whos
root is his directory in the /home folder. Users can only change thier own files, as the
administrator, you can work on all files of all users and the operating system. Eg:"su".
sudo Superuser Do. This gives administrative permission to an application. Eg:"sudo gedit" opens
a gnome text editor with permissions nesessary to change system files.
ls lists the contents of current dir, exept hidden files. "-a" list those too. "-l" adds info
like permissions.Eg: "ls -al"
cd Change directory. With many commands it's best to be in the right directory in order
to work on files within that directory. Eg: "cd Documents". Then using "ls" will show you
the contents of Documents.
Used alone it returns to root of current user. Eg:"cd",
except for su, who may need to use "/", eg:"cd /".
mkdir creates a new folder within the current dir. Use "ls" to confirm. Eg: "mkdir MyMusic"
cp copies a file, eg: "cp song.mp3 MyMusic" would duplicate song.mp3 into
the MyMusic dir.
It can copy whole directories: "cp /home/bob/MyMusic/ /home/fred/MyMusic/" duplicates Bobs'
music folder into Freds.
Note how the use of "/" indicates a folder. It can copy within the same dir if renaming.
eg:"cp song.mp3 song2.mp3".
mv moves or renames files. A rename eg: "mv song.mp3 song3.mp3" will rename the file.
A move eg:"mv song.mp3 MyMusic/" move the file from the current dir into
the MyMusic folder.
A move dir eg: "mv MyMusic/ /home/fred/freds_music/" copies your music folder into Freds
home dir.
rm Permanently deletes a file.
Eg: "rm song.mp3" Deletes that file. "rm *" removes all files in a dir. "rm G*" would remove
every file that starts with G.
rmdir deletes a directory, but only if it is empty. eg: "rm MyMusic", but you would first
"cd MyMusic" and rm the contents.
ps show all active processes with ID numbers of current user. Add -A for all processes of all
users. Eg: "ps -A"
kill kills the named process. Eg:"kill pidgin", or by PID number, Eg:"kill 4022". (You can find
the PID number with ps)
mount opens a partition or device for use. Filesystem and file"-t" must be included.
Eg:"mount -t iso9660 /dev/hdb/cdrom" would mount files(-t) on the CD filesystem(iso9660) of
a typical cdrom drive.
Eg:"mount -t ext2 /dev/sda3 /mnt" would mount files(-t) on a linux filesystem(ext2) on the
satadrive (sda) partition 3.
Eg:"mount -t vfat /dev/hda1 /mnt" mounts files(-t) on a Windows filesysem(vfat) on the IDE
drive(hda) partition 1.
note* many newer linux distros use the sata driver for both Sata and IDE hardrives, and
will show both as sda*.
umount unmounts a drive or partition. Eg:"umount/cdrom" unmounts the cdrom, or "umount/mnt"
unmounts a partition.
gzip compresses files into an archive. It works well with tarballs, eg:"gzip my_music.tar" would
compress your music tarball into "my_music.tar.gz". See also the (g) option of "tar".
tar packs files and directories into a single "tarball" file. Be sure to include the
create option "c", and always end options with the file option(f).
Eg:"tar -cf my_music.tar *.mp3" will create(-c) a file(f),the tarball called "my_music.tar"
with all .mp3 files in the current directory.
"tar" and "gzip" can be used together by using the "z" tar option.
Eg:"tar -czf my_music.tar.gz" would create(c) and compress(z) the file(f) my_music.tar.gz.
It must have the z option and the .gz extension.
To unpack an archive, the options change. (-czf packed it, -zxpf unpacks it)
Eg:"tar -zxpf my_music.tar.gz" will unzip(z), extract(x), preserve(p) permission and date
info, of the files(f). You could unzip without extracting if you still wanted the tarball
by ommiting the (x) option.
startx starts the loading of video and other hardware drivers. If you can't reach desktop, try
this first.
Before xorg 7.3, it was sometimes nessesary to reconfigure xorg;
X -configure | These are some possible commands for reconfigureing
Xorg -configure | xorg. If your kernel boots, but the system hangs or
xorgconfig | crashes before reaching your desktop, then running
dpkg-reconfigure -phigh xserver-xorg | one of these may fix it.


There are a few text editors available to bash. To edit a text file you can either cd to the
directory of the file and open, eg:"nano fstab", or if you know the path use that in the command;
eg:"nano /etc/x11/xorg.conf".

Nano is a text editor with instructions built in. If you're new to it, it helps to know
that the file commands are activated with the Cntl key.
Here are the locations of some commonly needed configuration files:
grub.conf /boot/grub/grub.conf Controls the boot order and boot/kernel options.
Now that UUID is the common format, grub needs the
partition listed as fstab shows it. See fstab.
Also check that the kernel is named the same as the kernel
in /boot.
fstab /etc/fstab Lists the mountpoints of drives.
xorg.conf /etc/x11/xorg.conf is the configuration file specifying what hardware drivers
to load, screen resolution ect...

Note, the original has spacing to make it clearer.......
Last edited by Ric on Wed Jun 10, 2009 13:28, edited 1 time in total.
Act weird. It supports freedom.
It gives others permission to act weird too.

User avatar
Fitzcarraldo
Sagely Hen
Posts: 8113
Joined: Sat Mar 10, 2007 5:40
Location: United Kingdom
Contact:

Re: A bash quick reference.

Post by Fitzcarraldo » Tue Jun 09, 2009 1:20

su doesn't mean "super user", it means "switch user" or "substitute user". It can be used to switch to any other user, not just the root user. For example:

Code: Select all

[[email protected] ~]$ su -
Password:
[[email protected] ~]# whoami
root
[[email protected] ~]# exit
logout
[[email protected] ~]$ su guest
Password:
[[email protected] fitzcarraldo]$ whoami
guest
[[email protected] fitzcarraldo]$ $ exit
exit
[[email protected] ~]$ 
Also, check out the following list of Linux cheat sheets, there are some good ones there that you might like to print off for your friend: All the Best Linux Cheat Sheets

User avatar
Fitzcarraldo
Sagely Hen
Posts: 8113
Joined: Sat Mar 10, 2007 5:40
Location: United Kingdom
Contact:

Re: A bash quick reference.

Post by Fitzcarraldo » Tue Jun 09, 2009 1:30

By the way, while on the subject of the su command, notice the difference between using "su" to switch to the root user and using "su -" to switch to the root user:

Code: Select all

[[email protected] ~]$ su
Password:
[[email protected] fitzcarraldo]# whoami
root
[[email protected] fitzcarraldo]# pwd
/home/fitzcarraldo
[[email protected] fitzcarraldo]# exit
exit
[[email protected] ~]$ su -
Password:
[[email protected] ~]# whoami
root
[[email protected] ~]# pwd
/root
[[email protected] ~]# exit
logout
[[email protected] ~]$ 

Ric
Simple Hen
Posts: 69
Joined: Sun Jun 03, 2007 2:25

Re: A bash quick reference.

Post by Ric » Tue Jun 09, 2009 3:13

About su = switch user. I stand corrected. Thanks, and thanks for the links to those cheat sheets too. Fosswires command reference is a must-have. I'm printing that one.:)
But what I had in mind was a bit more specific. I'm aiming for just the commands needed if a novice messed up and can't get to his desktop; fewer commands, but more info on the options needed.
Basicly just the commands I wish I had known when I used to mess up my grub, or xorg.
Act weird. It supports freedom.
It gives others permission to act weird too.

wolfden
Sharecropper
Posts: 9051
Joined: Sat Jan 14, 2006 0:55
Location: Midwest USA
Contact:

Re: A bash quick reference.

Post by wolfden » Tue Jun 09, 2009 3:50

Ric, this would be nice edition to the wiki, than you can format it better as it's really difficult to read that forum post and follow along.

Build up a comprehensive list for anyone to access and add. Aliases would be another good thing to add to it. For instance

alias pworld='emerge -avDN world'
alias eworld='equo update && equo world --ask'

micia
Sagely Hen
Posts: 2718
Joined: Wed Nov 26, 2008 16:41
Contact:

Re: A bash quick reference.

Post by micia » Tue Jun 09, 2009 8:52

Ric, if you have no time or can't add it to the wiki for any reason, I'll be happy to do it, just ask :D
(I'll translate it in Italian, anyway :P )

By the way, I can only notice an error in the nano example:

Code: Select all

nano etc/x11/xorg.conf
should be:

Code: Select all

nano /etc/X11/xorg.conf
even if it is just an example...

I'd add also:

shutdown -h now
turns off (h) your computer immediatly (now), you can add a timeout in order to let anyone
who is connected to your PC have the time to save its data, using a timeout instead of now.
eg:
- shutdown -h 10
Turns off your PC in 10 minutes.
You need to be root in order to use this command, and you can cancel a shutdown using ctrl+C.

reboot
reboots your computer, you need to be root in order to use this command.

find <path> -name <filename>
looks for files starting from <path> which are named <filename>.
eg:
- find /etc -name xorg.conf

if you want to suppress any error output (most commonly due to access denied) just redirect it
to /dev/null.
eg:
- find /etc -name xorg.conf 2> /dev/null

grep <word> <file1 file2 ... filen>
looks for <word> in each file, in this case in file1, file2 ... filen.
eg:
- grep Device /etc/X11/xorg.conf

it can be even more powerful, some useful combinations:
- grep -R Device /etc
look in any file starting from the directory "/etc" for the word "Device".
The -n option will output also the line number in which the word is found, for example:
- grep -nR Device /etc

you can also use meta characters:
- grep -n Device /etc/X11/*

Ric
Simple Hen
Posts: 69
Joined: Sun Jun 03, 2007 2:25

Re: A bash quick reference.

Post by Ric » Wed Jun 10, 2009 13:20

I could add those, and submit them to the wiki. But to be most useful I'll try to keep it down to commands that novices need to fix a system that doesn't boot. A few equo commands should be there too.
Act weird. It supports freedom.
It gives others permission to act weird too.

Post Reply