Newbie's Guide to Using a Shell (Linux, BSD, etc)

  [back to chrisheath.us ⤶]

originally posted: Aug 24, 2009

I have compiled a list of system commands that I felt were frequent enough to mention. The list covers commands that you use to interact with the file system and network, use the vi/vim editor, and utilize screen to make life easier.

Basic Shell Commands

Through this guide, I use <input description> to designate user input. Also, do not forget that you can use relative and absolute paths to files, though this guide usually assums the files we will be acting on are in the same directory.

Paths

Keep in mind that most of the examples below pertain to files and directories in the current working directory (where you are currently at in the filesystem; pwd), but you can also prepend the following path techniques to a file or directory, given to as a command argument, to get to a file or directory somewhere else on the filesystem.

. refers to current directory
.. refers to previous directory
/path/to/specific/file_or_dir absolute path to file/directory; relative to /
../../path/to/file_or_dir relative path; relative to current directory
~/path/to/file_or_dir file/directory relative to user's home directory

Files

ls list files in current directory
ls -a list all files and hidden (dot) files in current directory
ls -al verbosely list files in current directory
ls -lt verbosely list files in current directory and sort by modification time
cp <src_file> <dest_file> Copy file src to dest
mv <src_file> <dest_file> Move file src to dest
rm <file> remove file

Directories

ls <path to directory> list files in a specific directory
mkdir <dir_name> make a directory named dir_name
rm -r <dir_name> remove an empty directory
cp -R <src_dir> <dest_dir> copy directory src_dir to dest_dir
mv <source_dir> <dest_dir> move directory src_dir to dest_dir
mv <src_dir> <dest_dir> Move file src to dst

Networking

ping <hostname or IP> Try to bounce an ICMP packet off of a host (and verify reply); Used to check if a specific host is up (supposing the host replies to ICMP requests) or check your own network connectivity.
traceroute <host> trace the route of a packet from your host to <hostname or IP>
netstat  
route  
dig any <domain.tld> query various DNS records from a DNS server; see man dig
scp <file> <user>@<host>:~/path
scp -r <directory> <user>@<host>:~/path
securely copy a file to a destination via SSH
securely copy a directory to a destination via SSH
ssh <user>@<hostname or IP> securely connect a terminal to a remote computer
ifconfig display network interface information
wget http://domain.tld/path/file.tar.gz
fetch http://domain.tld/path/file.tar.gz
download a file from a host; note that http:// can be changed to various other protocols

File I/O Redirection

<command>   >  <output_file> truncates output_file and overwrites it with of command
<command>  >>  <output_file> appends output of command to output_file
<command1>  |  <command2> pipe the output of command1 to another command2
<command1>  <  <command2> direct the output of command2 into command1

System

cd <directory>
cd ..
cd
cd ~
cd ~<user>
change current working directory to directory
go back one directory
change current working directory to your home directory
change current working directory to your home directory
change current working directory to user's home directory
pwd print the directory you are currently in
tail <file_name>
tail -n 50 <file_name>
tail -f <file_name>
print the last few lines of file_name
print the last 50 lines of file_name
print the last few lines of file_name and scroll changes if they occur
head <file_name>
head -n 50 <file_name>
print the first few lines of file_name
print the first 50 lines of file_name
less <file_name> print file_name and navigate though the "pages" of output
tar -czf <filename.tar.gz> <directory>
tar -xzf <filename.tar.gz>
tar -xjf <filename.tar.bzip2>
compress a single directory with tar and gzip into an archive decompress a tar.gz archive
decompress a tar.bzip2 archive
su <user_name> temporarily switch to the user user_name
passwd <user_name> change user_names's password
adduser <adduser> initialize the adduser script to easily add a new user
write <user_name> or write <ttyname> broadcast a custom message to a user that is logged in
w
who
display tty/hostnames of logged in users
display tty/login time/idle time/processes logged in users
last
last -n 50
view previous logins
view the latest 50 previous logins
top verbosely display info about processes running and report various aspect of memory and CPU usage
cat <file_name> display the contents of a file
du [w/ sort]  
df -h display disk space usage statistics
man <command> look up the manual for the command command
exit log the user off
chmod 644 <file_name>
chmod +x <file_name>
chmod 755 <file_name>
let other users have read access to file file_name
make the file file_name executable
make the file file_name executable
chown <user> <file_name>
chown <user>:<group> <file_name>
chown -R <user> <directory_name>
grant user ownership privileges to file_name
grant user and group ownership privileges to file_name
grant user ownership privileges to directory and all file whith-in
chgrp <group> <file_name>
chgrp -R <group> <directory_name>
grant group ownership privileges to file_name
grant group ownership privileges to directory and all file whith-in
ln -s <source>
ln -s <source> <destination>
make a (symbolic) link to destination in current directory
make a named (symbolic) link to destination
stat <file or directory> display verbose attributes and information about a file or directory
uname -a
uname -r
verbose info about the type of operating system and kernel
operating system release level
stat <file or directory> display verbose attributes and information about a file or directory

Examples

files & directories

  • $ ls
  • mbox pr0g public_html
    file.txt fail.core other_file.c
  • $ ls -a
  • . pr0g mbox file.txt fail.core
    .. .screenrc .bashrc public_html other_file.c
  • $ ls -l
  • total 6
  • drwxr-xr-x   2 username   group   512   Oct   4 01:33 mbox
  • drwxr-xr-x   2 username   group   512   Dec   4 19:21 pr0g
  • drwxr-xr-x   2 username   group   512   Jan   4 22:05 public_html
  • -rw-r--r--   1 username   group   142   Mar   4 11:36 file.txt
  • -rw-r--r--   1 username   group   142   Mar   4 11:36 fail.core
  • -rw-r--r--   1 username   group   42153 Sep   4 14:36 other_file.c
  • $ cp file.txt file_copy.txt
  • $ mkdir trash
  • $ mv file.txt trash/
  • $ rm -rf pr0g
  • $ rm fail.core other_file.c
  • $ ls
  • mbox public_html trash file_copy.txt

system

CTRL+C stops the process you are in if it constantly outputs or is taking forever!

  • $ ping www.google.com
  • PING www.l.google.com (74.125.45.99): 56 data bytes
  • 64 bytes from 74.125.45.99: icmp_seq=0 ttl=50 time=38.834 ms
  • 64 bytes from 74.125.45.99: icmp_seq=1 ttl=50 time=39.126 ms
  • 64 bytes from 74.125.45.99: icmp_seq=2 ttl=50 time=44.004 ms
  • 64 bytes from 74.125.45.99: icmp_seq=3 ttl=50 time=39.347 ms
  • ^C
  • --- www.l.google.com ping statistics ---
  • 4 packets transmitted, 4 packets received, 0.0% packet loss
  • round-trip min/avg/max/stddev = 38.834/40.328130 ms

networking

CTRL+C stops the process you are in if it constantly outputs or is taking forever!

  • $ ping www.google.com
  • PING www.l.google.com (74.125.45.99): 56 data bytes
  • 64 bytes from 74.125.45.99: icmp_seq=0 ttl=50 time=38.834 ms
  • 64 bytes from 74.125.45.99: icmp_seq=1 ttl=50 time=39.126 ms
  • 64 bytes from 74.125.45.99: icmp_seq=2 ttl=50 time=44.004 ms
  • 64 bytes from 74.125.45.99: icmp_seq=3 ttl=50 time=39.347 ms
  • ^C
  • --- www.l.google.com ping statistics ---
  • 4 packets transmitted, 4 packets received, 0.0% packet loss
  • round-trip min/avg/max/stddev = 38.834/40.328130 ms

Using vi/vim

Most of the following things will work with vi, but note I am using vim during testing and in any examples. vim is vi-improved and is usually available on most systems! I will be using the two names interchangeably.

The vim editor is a editor that can be used in a terminal and, once learned, can be utilized to become an uber efficient and customizable editor for nearly any file! vim offers a plethora of features not covered here, but the following will get you doing basic tasks in no time at all.

First off, note that there are two modes (that we care about, atleast) that vim uses. command mode allows you use a large collection of built-in functions in the editor. These functions will either by typed in the vim command console or will be initiated by various key presses. insert mode allows you to input text as if you were in a normal editor.

When you open the editor, you will be automatically be in command mode. Press i to drop into insert mode and gain the ability to type text into the file. You can then hit ESC to get back into command mode.

Using screen

screen allows you have multiple terminals within a single terminal window and is able to make each screen session persist after you close and exit the terminal. This allows you, for example, to logout of a shell and come back at a later time and have the ability to resume a previous screen session and be back where you left off when you originally left it!

screen can be customized a number of ways by editing ~/.screenrc. I will provide my personal ~/.screenrc, but read up on it to get ideas for further customizations!

I have provided my own .screenrc file that you are welcome to use. To use it, just get it in your home directory.

If wget is unavailable to you, try using fetch in its place.

  1. $ cd
  2. $ wget http://chrisheath.us/nixGuide/screenrc
  3. $ mv screenrc .screenrc
^a then ' present a list of all screens in session
^a then [0-9] switch to the nth screen in session
^a then ^A switch to previous screen or toggle 2 screens
^a then A create a name for the current screen
^a then ^c create a new screen and switch to it
^a then C clear the current screen
^a then ^d detach (remove) the current screen
^a then ^n switch to the next screen
^a then ^p switch to the previous screen
^a then ^k kill all screen sessions and exit

chmod Permission Index

I have put together a basic reference of chmod permissions. Enjoy!

For a more complete "guide" to little things like this, check out my full guide to using a shell.

Digit R W X Result
0 - - - no access
1 - - x execute
2 - w - write
3 - w x write & execute
4 r - - read
5 r - x read & execute
6 r w - read & write
7 r w x read, write, & execute

R is read   W is write   X is execute

  [back to chrisheath.us ⤶]


Thanks to my friend Lyle Scott III for doing the original work for most of this guide!