0% found this document useful (0 votes)
60 views5 pages

Bash

The document provides an overview of basic Linux bash commands and shell usage including navigating directories, listing files, manipulating files and directories, setting permissions, running programs, and investigating commands. It covers commands like ls, cd, pwd, touch, cp, mv, rm, mkdir, chmod, chown, chgrp and explains shell expansion and setting the PATH variable.

Uploaded by

Premchand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views5 pages

Bash

The document provides an overview of basic Linux bash commands and shell usage including navigating directories, listing files, manipulating files and directories, setting permissions, running programs, and investigating commands. It covers commands like ls, cd, pwd, touch, cp, mv, rm, mkdir, chmod, chown, chgrp and explains shell expansion and setting the PATH variable.

Uploaded by

Premchand
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Linux Bash Basics

Joel Eidsath
April 4, 2008

1 Getting to a Shell
If you are logged into a graphical Linux session, you can generally call up the
command line by invoking “Terminal” or a similarly named application.
If you do not wish to invoke a graphical session at all, you can use the
“Ctrl-Alt-F1” key combination to go directly to a command line login prompt.
“Ctrl-Alt-F2” will take you to another prompt that you can use simultaneously,
and so on with “F3, F4, etc.” To get back to the graphical session, the key
combination is generally “Ctrl-Alt-F7” or “Ctrl-Alt-F8.”

2 Shell Basics
There are a number of different command line environments. The most common
is probably “BASH,” and that is the only one that will be covered here. The
default shell can usually be set using the “chsh”, change shell command.
The first thing that you will see upon logging in is a command prompt:

-bash-3.1$

Here, user “thras” is logged into the computer “mimir.” The current direc-
tory can be found with the “pwd”, print working directory command.

2.1 Getting Around


-bash-3.1$ pwd
/u/thras

The “cd”, change directory command is used to move to another directory.

-bash-3.1$ cd /
-bash-3.1$ pwd
/
-bash-3.1$ cd /usr/sbin
-bash-3.1$ pwd
/usr/sbin

1
The “ls”, list directory contents command is used to see the files in a direc-
tory.

-bash-3.1$ cd /usr
-bash-3.1$ ls
bin/ games/ kerberos/ libexec/ sbin/ src/ X11R6/
etc/ include/ lib/ local/ share/ tmp@

So far, we have been using full pathnames to specify directories. When we


want to travel to the “sbin” subdirectory of “usr” we have typed “cd /usr/sbin”.
This is always necessary. If the first slash of a directory name is left off, it
becomes a relative name. Thus the following commands are equivalent:

-bash-3.1$ cd /
-bash-3.1$ cd /usr/sbin
-bash-3.1$ pwd
/usr/sbin
-bash-3.1$ cd /
-bash-3.1$ cd usr
-bash-3.1$ cd sbin
-bash-3.1$ pwd
/usr/sbin

To go to your home directory, you can use the shortcut “ ” which will bash
will replace in the command line with your home directory before executing.
The following commands are equivalent (for user thras):

-bash-3.1$ cd /u/thras
-bash-3.1$ cd ~

2.2 Files
As we have seen, “ls” is used to see the files in a directory. “ls” has a number
of options which can be found by using “man ls”.1 The two most important
options are “ls -l”, which will show details about each file, and “ls -a”, which
will show hidden files (those file names which begin with a “.”). The two flags
can also be used together “ls -la”.

-bash-3.1$ ls -l
total 0
-rw------- 1 thras thras 0 Apr 4 10:56 afile
-bash-3.1$ ls -a
./ ../ afile .bfile
-bash-3.1$ ls -la
total 16K
drwx--x--x 2 thras thras 4.0K Apr 4 10:58 ./
1 More on man later

2
drwx--x--x 70 thras tcc 12K Apr 4 10:58 ../
-rw------- 1 thras thras 0 Apr 4 10:56 afile
-rw------- 1 thras thras 0 Apr 4 10:58 .bfile

3 File and Directories Operations


To create empty files, the command “touch filename” can be used. “cp file1
file2 ” makes a duplicate, and “mv file1 file2 ” renames the file (deleting the
original). To get rid of the file afterward “rm filename” is used.
In order to create directories, the command “mkdir directoryname” is used.
To delete that directory you can use “rm -r directoryname”. “rm -r” will give
an “Are you sure?” prompt whenever it deletes a file. To make “rm -r” run
silently, try “rm -rf”. (What does “rm -rf /” do?)
Often, you will need to work with more than one file at once. Shell expansion
is how bash allows users to do this efficiently.

-bash-3.1$ ls
afile bfile cfile dfile nofile

3.1 ? Expansion
Match one character of a filename

-bash-3.1$ ls ?file
afile bfile cfile dfile

3.2 * Expansion
Match any string.

-bash-3.1$ ls *afile
afile
-bash-3.1$ ls *file
afile bfile cfile dfile nofile

3.3 [] Expansion
Matches any characters inside []

-bash-3.1$ ls [abc]file
afile bfile cfile
-bash-3.1$ ls [a-c]file
afile bfile cfile

3
4 Permissions
“ls -l” returns a detail view that includes a file permissions line at the start. It
will usually be something like “- r w - - - - - - -”.
The most important characters in this line are “r,w,x”. “r” makes the file
readable (or listable for a directory), “w” makes the file writable, and “x” makes
the file executable (or navigable for a directory). The permissions line is split
up into four sections “- k - - - k - - - k - - -”. The second section is permissions
for the owner of a file (the user named in “ls -l”), the third section is for the
group (again named in “ls -l”) and the fourth section is for everyone else. The
first section will be “d” for a directory, and can sometimes indicate special
permissions (sticky, setuid, setgid – see “man chmod” for more information).
The easiest way to interact with these permissions is to mentally translate
the r’s, w’s, and x’s, into octal digits. In this case, r=4, w=2, and x=1. So rwx
will be 4+2+1=7. In order to set permissions on a file to rw (=6) permissions
for the main user, and r (=4) permissions for the other users, the following
command will be used: “chmod 644 filename”.
Similar commands exist for changing the owner (chown) and the group
(chgrp) of a file. In general, the root permissions are needed to use chown
(why?) or to use chgrp for a group that a user is not part of.

5 Programs and Paths


In order to execute a program, it needs to have its executable permission set.
The command is just: “/fullpath.../filename”.

-bash-3-1$ ls
aprogram
-bash-3-1$ pwd
/u/thras/test
-bash-3-1$ /u/thras/test/aprogram
Hello World!
-bash-3-1$ ./aprogram
Hello World!

As you can see, “.” is a shortcut for the full path when you are already in
the directory containing the program.
If you don’t want to type in the full path of a program every time, it can be
added to the “$PATH” variable associated to your session.

-bash-3-1$ export PATH="$PATH:/u/thras/test"


-bash-3-1$ aprogram
Hello World!

If this is done in the “.bashrc”, the new PATH will be there every time a
user logs in.

4
6 Investigating on your own
The internet is a great resource for how to guides and tutorials. If you are trying
to find information on a specific command, however, the best resource is usually
“man command” or “info command”. Man pages also exist for c libraries, config
files, and basic system concepts. Often commands have multiple man pages. If,
when you type “man foo”, there is a section with “SEE ALSO foo(5)”, you can
get to it by typing “man 5 foo”.
“info” is a much more advanced system, that is invoked just like man. If
you need a tutorial or examples, “info” can be a better bet than man. However,
there are generally far fewer “info” topics available than “man” topics.

7 Problems
1. How can you change directory to the last one visited?

2. How can you make “ls” format its output in columns? In colors?

3. What might be dangerous about changing a PATH to include the new


directory at the beginning?

4. How can you force “mkdir” to create new parent directories as needed?

You might also like