This is not a page to learn how to program in bash, sh, csh, ksh, or
anything else like that. This page is to list some basic commands that
are necessary for navigating the filesystem, modifying that filesystem,
and doing some other basic things. I've put them here in order of what I
think is important, not in alphabetical order. Sorry if that throws some
of you off.
Some of these commands are built into 'bash', and that's the command
interpreter that I prefer, and the one that I teach. If you're using
something else, then your mileage may vary on what you're trying to do.
- Tab Completion: When using bash as your shell, you can type part of a command, and then hit [TAB] to finish out the command. If you're going for the command 'gunzip', you can type 'gun' and hit [TAB] to get 'gunzip'. The same thing can be done for files. Let's say you want to run the command 'gunzip some_really_long_file.gz'. You can type 'gun[TAB] some_really[TAB][ENTER]'. If multiple files match, you'll get a bell (maybe, depending on your term settings). Hitting [TAB] again will show all matches, so that you can refine your search.
- Wildcards: When performing actions on files, you can execute them on multiple files easily by using wildcards.
- If you have a bunch of files ending in '.c', you can do something like this 'tar cvfz *.c'. The '*' will match all files named, and the '.c' part tells the shell finish off with anything ending '.c'. '*' by itself will match all files.
- If you want to match a single character, you can use '?'. Doing a command like 'ls foo.?' will match all files with names like 'foo.c', 'foo.h', 'foo.b', etc.
- The most complex wildcard setup matches characters that you specify. You can do something along the lines of 'wc -l foo.[ch]' to see how many lines are in 'foo.c' and 'foo.h'.
- Wildcards can be used in conjuction with one another. One command that I type quite often is 'wc -l *.[ch]' that tells me how many lines are in every file that ends in '.c' and ends in '.h'.
- Redirection and Pipes: Output from one command can be sent to an output file, or another command.
- To output the results of a command to a file, you can run 'ls > ls_results.txt'. Doing this will overwrite any existing file (which is sometimes what you want.) To append the results of a command to an existing file, you can do 'ls >> ls_results.txt'.
- To send the output of one command through another command, you use a pipe (|). To see what piece of code has the most lines in it, you can do 'wc -l *.[ch] | sort -n'. This takes the output from the 'wc' command which tells you how many lines are in the files, and then the 'sort' command numerically sorts the output of the 'wc' command. There is no practical limit to the number of pipes that you can use. To see a sorted list of all of the accounts on a server, you can do something like this: cat /etc/passwd | cut -f1 -d: | sort
- Directories:
- cd: 'cd target' will change your working directory to 'target'.
- cd ~: Will change your home directory to your home directory. It's the same as 'cd' with no arguments.
- cd -: Changes to your last directory. Allows for easy swapping between two directories. (May be a bash only thing, not sure.)
- mkdir: 'mkdir target' will make a directory named 'target'.
- rmdir: 'rmdir target' will remove 'target' if it is empty.
- pwd: Shows the directory that you are currently in.
- Files:
- touch: 'touch foo' will create an empty file named 'foo'.
- cp: 'cp foo bar' will copy 'foo' to 'bar'. If 'bar' is a directory, it will copy 'foo' into 'bar'. You can copy multiple files into a directory with a single command, but the last argument must be a directory (e.g.: cp file1 file2 file3 file4 target_dir). If 'bar' is a file, then 'foo' will overwrite the file.
- mv: 'mv foo bar' will move (rename) 'foo' to 'bar'. If 'bar' is a directory, then 'foo' will be moved to 'bar'. If 'bar' is a file, then 'foo' will overwrite 'bar'.
- rm: 'rm foo' will remove 'foo' from the filesystem. There is no 'recycling bin', 'trash can' or anything else like that. Once a file is rm'd, it is gone. Don't email your system administrator asking for him to restore the file unless you know for sure he makes regular backups of your files.
- ln: 'ln -s foo bar' Creates a link to a 'bar' named 'foo'. (more details)
- chmod: 'chmod [ugo][+-][rwx] [filename]' will change the permissions of the file. (more details)
- chown: 'chown user:group foo' will change the user owner to 'user' and the group owner to 'group' for file 'foo'. (more details)
- ls: 'ls' shows the files that are in the curently directory. There are approximately 3.2 million switches that you can give 'ls' to do different things. I would suggest spending some time with 'man ls' to find out what you need. Switches that I commonly use are:
- ls -lrat: shows all files, sorts by last modified time.
- ls -a: shows hidden files.
- ls -d foo: lists 'foo' if it's a directory, but not the contents.
- ls -lR: lists all files in long listing and recurses through all subdirectories.
- ls -1: lists files with each file on a single line (good for `ls -1 | wc -l` to see how many files are in the directory).
- cat: 'cat foo' will display the contents of file 'foo'.
- head: 'head foo' will show the first 10 lines of 'foo'. 'head -100 foo' will show the first 100 lines of 'foo'.
- tail: 'tail foo' will show the last 10 lines of 'foo'. 'tail -100 foo' will show the last 100 lines of 'foo'.
- more: 'cat foo | more' will display the contents of 'foo', but will display pauses (and wait for input) with each page.
- less: 'cat foo | less' does the same as more, but has more features, such as the ability to scroll up and down.
- grep: 'grep text foo' will display all lines of 'foo' that contain the string 'text'. There are quite a few options for grep. Make 'man grep' your friend.
- sort: 'cat foo | sort' will display 'foo' sorted alphabetically. 'cat foo | sort -n' will display 'foo' sorted numerically.
- uniq: 'cat foo | uniq' will display all unique lines of 'foo'. Some versions of 'uniq' require the data to be sorted, so you may need to do 'cat foo | sort | uniq'.
- wc: 'wc -l foo' will show how many lines are in 'foo'. wc also accepts '-c' for bytes, '-m' for characters, and '-w' for words. Writing an essay that has to be at least 8,000 words? A quick 'wc -w foo' will tell you.
- find: Powerful tool that finds files on the file system based off of a wide variety of parameters. 'man find' is a decent resource. The basics are 'find [starting_point] [parameters] -print'. 'find . -name \*.pas' will find all files in the current directory (and subdirectories) named '*.pas'.
- locate: 'locate foo' will find all files on the entire filesystem that contain the string 'foo'. Good tool for finding where a file is located.
- which: 'which ls' will show you where 'ls' is located, but only if 'ls' is in your $PATH variable.
- du:: 'du foo' will show how much disk space is taken up by 'foo'.
- file: 'file foo' will tell you what kind of file 'foo' is.
- ldd: 'ldd foo' will show you what libraries 'foo' depends on if 'foo' is a binary file.
- Miscellaneous:
- passwd: Interactive tool for changing your password.
- exit: Logs you out of the server.
- alias: 'alias cdht "cd /opt/www/htdocs"' would allow you to type 'cdht' to execute the longer command of 'cd /opt/www/htdocs'.
- unalias: 'unalias cdht' would remove the 'cdht' alias.
- man: 'man foo' would show you the manual for 'foo'. Keep in mind that 'man' is typically written by engineers for engineers. If you're just learning a command for the first time, a well crafted search on Google will do you more good. If you already know the command, but need a refresher on how to use it, then 'man' is the route to go.
- date: Shows the current date on the server.
- history: Shows your last 1000 commands. It's a good idea to do 'history 100' to see the last 100 commands.
- echo: 'echo foo' will display 'foo' to the terminal.
- sed: sed is an inline text processing tool. I typically use it for replacing text like: 'cat foo | sed "s/bar/bar2/" to echo 'foo', but replace all instances of 'bar' with 'bar2'.
- awk: awk is another inline text processing tool. I will usually use it to process the output of 'ps' to find process id. The command for that would be something like: "ps -ef | grep httpd | awk '{print $2}'".
- xargs: Takes all args passed to it, and runs a command on that list. Something like 'ls *.c | xargs touch' would be the same as 'touch *.c'.
- source or .: 'source foo' or '. foo' executes 'foo' in the current environment. It's handy when you change your .bash_profile and need to reload it without logging in again. You can do '. ~/.bash_profile' to reload it.
- who: Shows who is logged it.
- w: Same as 'who' but with more details.
- users: Just shows the usernames of the people that are logged in.
- whoami: Shows your username. Good for system administrators with multiple accounts. Lets them easily see who they are currently logged in as.
- frm: Command on my server to summarize everything that is in your Inbox.
- Archives:
- tar: Creates/Extracts tarballs, which are uncompressed archives of files. 'tar cf foo.tar files' will create 'foo.tar' that contain the list of files. If you want verbose output do 'tar cvf foo.tar files'. If you want the tarball compressed (with gzip) on the fly, do 'tar cvfz foo.tar.gz files'. If you want to extract a tarball, do 'tar xvf foo.tar'. If the file is compressed, then you will need to do 'tar xvfz foo.tar.gz'.
- gzip: 'gzip foo' will compress 'foo' and rename it to 'foo.gz'.
- gunzip: 'gunzip foo.gz' will uncompress 'foo.gz' and rename it to 'foo'.
- bzip2: 'bzip2 foo' will compress 'foo' and rename it to 'foo.bz2'.
- bunzip2: 'bunzip2 foo.bz2' will uncompress 'foo.bz2' and rename it to 'foo'.
- zip: 'zip foo.zip files' will create a compressed archive 'foo.zip' containing 'files'. Please use 'tar cvfz foo.tar.gz files' instead. You get better compression, and if you are sharing with other Linux people, they will like you better.
- unzip: 'unzip foo.zip' will extract all files in 'foo.zip' in the currect directory.
- NOTE ON GZIP AND BZIP2: gzip and bzip2 basically do the same thing. You'll get better compression from bzip2, but gzip has been around much longer, and is the de facto standard compression in the UNIX/Linux world.
- Process Control: When dealing with processes, there are two things to keep in mind. What process id (PID) and parent process id (PPID) you are dealing with. When running 'ps -ef' the second column is the PID, and the third column is the PPID.
- ps: Shows current running processes. To see your processes, run 'ps'. To see all running processes run 'ps -ef'. Like 'ls', 'ps' has many, many switches that can be passed to it. See 'man ps' for more details.
- pstree: Shows processes in a tree format that shows parentage, zombies, orphans, and ancestry of the running processes. I typically call it with 'pstree -pa username' to see all processes running that are owned by 'username'.
- top: Interactive utility that displays important information about processes that are running. If you suspect that a process is hogging system resources, top will show you. Please use sparingly as top is itself a resource hog. Do not leave a top process unattended. It will only upset your system administrator.
- kill: Used to kill a process id. Get the process id from 'ps'. If the process won't die, use 'kill -9 PID' where 'PID' is the process id.
- killall: If you know the name of a process (e.g.: httpd) you can kill all process with a name, by doing 'killall httpd'. If the process won't die, you can always try 'killall -9 httpd'.
- ulimit: This controls how much of the system your processes can use. I usually use it when I have a crashing process that won't generate core dumps for debugging. The command 'ulimit -a' will show current settings. The command 'ulimit -c unlimited' will allow crashing processes to produce core dumps.
- Programming:
- make: This command processes a file called 'Makefile'. Makefiles can be used to automate tedious processes, or control the complicated process of compile software with all of its dependancies. This page is a decent starter on how to create Makefiles.
- diff: 'diff foo bar' will show the differences between 'foo' and 'bar'. It's great for finding the differences in code when multiple coders are working on the same file. For complex situations, it's best to use something like DCVS to control code differences.
- patch: patch takes the output from diff and allows you to merge the patch into your codebase. The command is usually invoked something like 'patch -p0 < foo.patch'.
- gdb: gdb is a great tool for attaching to running processes, running processes in a controlled environment, or deugging a core dump after a process crashes. gdb is much too complex to cover here. Here is a good gdb tutorial for those that need it.
Last Modified: 2006-04-19
Copyright © 2006-2019 - John Evans