30
COMMAND LINE TOOLS by David T. Harris

Command Line Tools

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

Page 1: Command Line Tools

COMMAND LINE TOOLSby David T. Harris

Page 2: Command Line Tools

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.

Page 3: Command Line Tools

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.

Page 4: Command Line Tools

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

Page 5: Command Line Tools

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 -

Page 6: Command Line Tools

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

Page 7: Command Line Tools

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)

Page 8: Command Line Tools

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.

Page 9: Command Line Tools

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

Page 10: Command Line Tools

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.

Page 11: Command Line Tools

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.

Page 12: Command Line Tools

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>

Page 13: Command Line Tools

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>

Page 14: Command Line Tools

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.

Page 15: Command Line Tools

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.

Page 16: Command Line Tools

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.

Page 17: Command Line Tools

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.

Page 18: Command Line Tools

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>

Page 19: Command Line Tools

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

Page 20: Command Line Tools

Viewing Files

cat cat ~/.bashrc

Pagers (more is less) more more ~/.bashrc

less less ~/.bashrc

Page 21: Command Line Tools

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.

Page 22: Command Line Tools

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

Page 23: Command Line Tools

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

Page 24: Command Line Tools

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

Page 25: Command Line Tools

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.

Page 26: Command Line Tools

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

Page 27: Command Line Tools

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

Page 28: Command Line Tools

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>

Page 29: Command Line Tools

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.

Page 30: Command Line Tools

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.