Upload
david-harris
View
510
Download
3
Embed Size (px)
DESCRIPTION
Introduction to command line tools for *NIX (UNIX (like OS X and Solaris/SunOS), BSD, & GNU/Linux) environments. I made this presentation originally for the LUG@UCF when I was an undergrad but still contains valid information. Hope you find it useful.
Citation preview
COMMAND LINE TOOLSby David T. Harris
Why?
Efficiency No trying to find where an option is on a gui. Speed Barely any graphics -> little overhead. Ease of Use Generally simple one line commands Scriptable If you do have a sequence of commands to do you can place them in a script. Power Those one line commands can perform multiple tasks. Flexibility The abiliy to use multiple commands together. The ability to combine switches/options.
Finding Help
Man pages man <programName> GNU Info info <programName> /usr/share/doc cd /usr/share/doc/<programName> --help or -help <programName> --help OR <programName> -help help When inside bash, just type help to see a list of bash topics bash can help you
on.
Note: All of these options are not available with every program.
Moving Around
Change Directory Directly cd </path/to/a/directory>/starting/from/the/root/directory> Example:
Let’s say that you’re currently in /usr/share/doc/vim and you want to go into /usr/share/doc/w3m Then you would issue: cd /usr/share/doc/w3m
Relatively cd <nameOfAdirectoryInYourCurrentDirectory> cd ../<nameOfAdirectoryInYourParent’sDirectory> Example:
Let’s say that you’re currently in /usr/share/doc/vim and you want to go into /usr/share/doc/w3m Then you would issue: cd ../w3m
Usefull symbols
/ Your root directory if positioned at the beginning of a path.A directory dividor if placed after the beginning of a path.
. Your current directory
..Your parent directory
~ Your home directory
pwdA command that shows you the value of your current working directory
-A symbol that stands for your previous working directoryExample:
If you wanted to go to your previous working directory you would issue:cd -
Viewing Files
ls (list) -l long listing (lists file permissions, owernship, size, date, time, & name )
-h list the filesizes in human readable format (MB, KB, etc...) Must be used in conjunction with "-s" or "-l"
-t list according to last accessed time (date)
-i list the inode values
List hidden files ls -d .* List hidden directories ls -d .*/ List directory files recursively (WARNING: List may be very long) ls -R
File Information
file Gives the true identity of a file. This can be very usefull since in *NIX most files don’t need extentions. Note: Some programs (like gcc) will only work correctly on files with a specific
extention.
stat Gives a good amount of information on a file including the last acces, modified,
and changed time. Example: stat .bashrc Note: See man 2 stat(Programmer man section) for the differences in modified
(mtime) and changed times (ctime)
Executable Program information
type It tells you if the passed in program name is an alias, function, builtin command,
reserved word, disk file, or not found.
ldd This program tells you all the shared libraries that a program uses when it’s
executed. Note: If a library does not show up in the output, and you know that you just
recently installed it, you probably should run ldconfig to have the computer configure your installed shared libraries.
Wildcards
* Lists 0 or more matching characters. ? List 0 or 1 matching characters. Globbing [] Example: List all files beginning with the word bob and ending with a digit.
ls bob*[0-9] ls bob*[0123456789]
Note: Globbing does not work with the first dot in hidden files. Reference: http://www.faqs.org/docs/abs/HTML/globbingref.html
Change File Permissions (chmod) Part 1
4 Read only access (r) 2 Write only access (w) 1 Exectable Only access (x) 0 No permission to do anything.
You add the first 3 numbers together to decide what permission value you wish for a single set of permissions for a file.
There are 3 sets of these values for every file.
chmod Part 2
When you do an ls -l on a file you’ll see something like -rwxr-x-w-
From left to right these sets are as follows: 1. Owner permissions These are the permissions that dictate what the owner can do to a file. 2. Group permissions These are the permissions that dictate what everyone in the same group as the
owner, can do with a file.
3. World permissions These are the settings that everyone else (that is not the owner of the file, and
is not in the same group as the owner of the file) can do with a file.
chmod - Part 3
Examples:
If you want only yourself to be able to read, write and execute a file, but you want everyone in your group to only read and execute the file, and everyone else to just execute the file, then you’d set:
chmod 751 <file>
Change File Ownership (chown)
When you do an ls -l, after you see the permissions of a file you’ll see the owner of the file followed by the group of the file.
If you wish to change either the "owner" or the "group" of the file", you can do so by using chown.
Example1: Say you have a file that is owned by root, and has a group of root. You wish to change the file’s ownership such that user "bob" owns it, and that it belongs to the group "users".
chown bob:users <file>
Chown (Continued)
If you just want to change the group you would do: chown :users <file> Similarly if you just want to change the owner you would do: chown bob <file>
Note: You might have to be su’d to root to perform some of these operations.
Copying Files (cp)
To a different directory cp <fileToCopy> </path/> Example: cp ~/.bashrc ~/DOCS/Defaults/ Note: In this example a DOCS/Defaults directory needs to exist in your home directory.
To another file cp <fileToCopy> <copyOfOriginalFile> Example: cp ~/.bashrc ~/.bashrc-orig
Note: If you do not have "cp -i" set up as an alias in your configuration file, you may accidentally overrwite a prexisting file.
Moving Files (mv)
To a different directory mv <fileToMove> </path/> Example: mv ~/DOCS/.bashrc ~/DOCS/Defaults Note: In this example a DOCS/Defaults directory needs to exist in your home directory.
To another file (renaming) mv <fileToRename> <renamedVerionOfOriginalFile> Example: mv ~/.bashrc-orig ~/.bashrc-March-3-2007
Note: If you do not have "mv -i" set up as an alias in your configuration file, you may accidentally overrwite a prexisting file.
Deleting Files (USE WITH CAUTION!!)
rm (Remove) Example: rm *~ This will delete all of your the temporary files (made by vim) in your current
directory.
Note: When you start out using the rm command it is generally safe to put an alias "rm -i" into your shell’s configuration file. This will prevent you from accidentally deleting a file forever. It is insanely hard and at times nearly impossible to retrieve files once they’ve been rm’d.
Making Directories
Make a new directory in your current directory mkdir <nameOfNewDirectory>
This command creates any directories missing on the path to the directory you wish to create.
It does not do anything to preexisting directories. mkdir -p </path/to/a/new/dir/nameOfNewDirectory>
Deleting Directories
rmdir (Remove directory) If you have an empty directory you want to delete you can use this. Example: rmdir ~/testDir If you have files in this directory it will tell you.
Note: If you want to delete a directory with files, you have to do: rm -fr ~/testDir -f //force -r //recursively go through all subdirectories
Viewing Files
cat cat ~/.bashrc
Pagers (more is less) more more ~/.bashrc
less less ~/.bashrc
Background Processes
At times it’s usefull to put processes in the background so that you can continue to work at the command line.
<command> & Place the command into the background. fg Brings the last command you backgrounded into the foreground jobs Lists your backgrounded processes. Note: If you have more than 1 process backgrounded, and you
wish to foreground a process other than the last process you backgrounded, you can do so, by specifying the job number.
Finding files
locate Locate keeps a database file on your computer which maintains the locate of
files on your computer. In order to find something using locate you’d type: locate <somePieceOfAFilename>
Example: locate vim Note: If your computer doesn’t already, you should update your locate database
on a regular basis, to prevent locate searches from returning invalid or old/stale information.
Updating your locate database: 1. su to root 2. updatedb & Note: The & puts the process in the background. If you wish to bring it back to the foreground type fg
find
At time locate might not be able to find a file that you recently created, if you haven’t already updated your database since creating the file. Also locate at times doesn’t include certain directories (like mounted directories ).
Hence we’d want to use find. Say we want to find all files that begin with the word bob starting from our
current directory and including all subdirectories. We’d issue: Example find . -iname "bob*"
-iname //case insensitive filename .//current directory
which and whereis
which This tells you the path of an executable file Example: which ls whereis This will also tell you the path of an executable file, the location of
it’s man page, and the locations of it’s src file (if available). Example: whereis ls
Finding text in files using grep
grep (GNU Regular Expression Parser)
If you want to find text in files than you want to use grep.
Example: grep -ril ’default’ * This will look at every normal file in your current directory and it’s subdirectories
for the string ’default’ in all of it’s possible cases. -r //recursive -i //case insensitive -l //only list the filename - not the line of the file where the pattern was found.
Combining commands using |
How it works: When two programs are separated by a pipe, the output of the first program
becomes the input of the second program.
Example: ls and less At times a directory listing can span multiple pages, in this case in order to go through the output without
paging up you’d: ls|less
locate and grep Say you want to find vim files on your computer but only in /usr then you’d do something like: locate vim|grep /usr
Becoming a different user
su This allows you to be a different user. By default it allows you to become root.
Once you execute "su" you’ll need to enter that other users password. By default you’ll have to enter root’s password.
Example: su bob
su - This will inherit the path of the user you’re becoming. Example: su - bob
Viewing processes
ps Just view your own processes running in your shell. ps aux View all processes top View all processes pgrep -lf <nameOfProcess> Search the group of running processes for <nameOfProcess>
Stopping/killing processes
kill <pidOfAProcessYouWishToKill> Example: kill 1501
-9 <pidOfAProcessYouWishToKill> Example: kill -9 1501
killall <nameOfprocess> Example: killall firefox
pkill <nameOfprocess> Example: pkill firefox pkill -9 firefox
Note: The -9 switch should only be used for obstinate processes which just won’t stop running.
Finding information about your computer
/proc This has a series of directories that contain various information on your system
including memory, what processes are running, etc...
dmesg This is a very usefull tool to diagnose problems with devices on your system.
This is a standard unix tool, whereas /proc is being eliminated from the default install for most *BSD OS’s.
lspci -v This is a linux tool. It displays the device attached to your system (graphics
card, network controller, etc...) Note: In order to see some information you have to be root.