Basic Unix Commands
Below is a selection of basic unix commands that will span Solaris, HP and AIX. We hope that you find these command useful.
| banner calendar cat copy date echo file head id link list more move octal dump remove sort tail translate uniq who whoami who am i word count composite commands C shell commands Filters |
banner
BANNER copies a string or variable into an expanded text banner
banner ulrika
banner "hello I'm over here" - text with white space must be enclosed by quotes
calendarCAL calender returns the days and dates of the selected month and year
cal By default shows current month's details
cal 2000 shows the details (all 12 months) of year 2000
cal 8 1999
cal 8 2010 Check out your invoicing dates in August 2010
catCAT concatenates (LATIN - to join together) a file and displays its contents
cat joe displays file contents of joe
cat > newfile allows input directly to the newfile created
Ctrl-d makes the file empty (if it already existed) - then input text/commands etc
Ctrl-d quits the file/command/data input sequence
cat file1 file2 > newfile merges the two input files into file newfile
copyCOPY copy one file another file and retain the original file
cp file1 file2
cp file1 file2 ~sven copy file1 and file2 to user sven's home directory
cp -r /export/home/john /export/home/sue
datedate specifies the day, date, time, time zone and year date
echoECHO useful for sending diagnostic messages to standard output or a named file.
echo hello returns hello to screen
echo "hello there" if there is white space it must be made into a string.
echo $HOME $SHELL $TZ echoes the values of built-in shell variables:
/export/home/vincent /bin/ksh EIRE-GB
FILE displays the category of the named file
filefile newfile returns a message saying that newfile is a text file (if it is a text file)
file /export/home/helen returns export/home/helen is a directory
Understanding if a file is a text, data or binary file is important in how we manage that file.
You'll see what I mean if you do cat /usr/bin/ls - cat is a command that works on a text file
/usr/bin/ls is a binary file - they don't get on... beep beep beep
headhead displays the first 10 lines of a file by default
head -25 rhyme displays the first 25 lines of a file
idID
id displays user and group ID for the current user
id -a show secondary group information for the current user
id -a sven user and group info plus secondary group information for user sven
lnLINK
is used to create a new filename and link it to the original file, i.e. to give a file a second or third + name. The link count keeps track of the number of filenames to which a link file is linked. A zero link count indicates that the file has been deleted.
listLIST list file to screen/output device
ls -l l - stands for long
ls -a a - lists hidden files (dot, and dot dot files)
ls -s s - adds a field to the start of a file in blocks (usually 512 bytes)
ls -i i - gives i-node details
ls -las las - stands for everything
ls -l db.dat db.inx - gives information only on the files specified
ls -l gl_data.?? - lists all gl_data. files with any two character suffix.
ls .??* - lists all hidden files
ls *.c - matches all C Language source code files that end with .c
ls ar_*.dat - lists files starting ar_ ending with .dat
ls {ar,ap,sa}data.* - lists all files beginning arcdata. , apdata. , sadata.
ls s{a,e,i,o,u}{n,t} - expands to files starting san,sen,sin,son,set,sit,sot,sut
ls -l *tt* - will list all files whose filenames contain tt
? - the question mark character represents one character only
ls -l lett? - would list files like lett1, lett2, lett3, but ignore letters, letter, lettuce as it only searches for 1 character after tt
[ ] - these parenthesis are used to enclose a whole string
ls -l [ayegj]* - will list all files whose names start with a,y,e,g or j
ls -l [n-z]* - lists all files whose names start with a letter between n and z
! - the exclamation mark character used inside square parenthesis has a negating ;effect
ls -l [!shrp]* - lists all files whose names start with any characters except s,h,r,p
ls -l [!l-z]* - lists all files whose names don't start with a letter between l and z
ls -l .appreg.x.?????????? - list all .appreg files with DDMMYYHHmm suffix
ls -l .appreg.x.??? - list all .appreg files with 3 character suffixes
ls -l .appreg* - list all files with .appreg. prefix
ls *.[co] - list all files with file endings .c or .o
ls *t* - list all files with ‘t’ in the title.
ls FC - returns files with file type flags in multiple columns
ls -a - lists all files including dot files
ls -A - lists all files except dot files
ls -l - lists files in long format, showing permissions, owner, size, date and full filename.
ls -lt - same as above but sorted in date order.
ls ltr - same as above but sorted in reverse date order.
ls -l db* the * represents any missing character in a filename, or can represent no characters at all
moreMORE displays files one screen at a time
more file1 q for quit return for one more line space for one more page
moveMOVE FILE renames file to a new file with a different name
mv ted hatty
mv dir1 dir2 moves dir1 to dir2 - note that both directories must both have the same parent directory
octal dumpOD displays an octal dump of the file specified
od joe
od -b joe - not currently available
od -c joe - displays characters
od -d joe - displays decimal characters
composite commandswho | sort sorts who is logged on into alphabetic order
cat jon joe | wc -l does a word count on the merged files of jon and joe
sortSORT sorts files as specified (alpha by default) but also numeric when specified with -n
sort file1 > sortedfile1 executes an alpha sort of the contents of file1 and creates a sorted output file called sortedfile1
sort huge > naffile & sorts as above and the & character allows the job to run in the background - it will issue a job number like [3329]
sort - sorts files according to one or more keys in ascending/descending alphabetic/numeric order
sort [options] [ sort-field-information] [file list]
-n numeric sort
-r reverse order sort
-o filename send the output to the file called filename
sort +1 -2 names assuming that the field count starts at 0, + means "move across one field before starting to sort"
sort -n +2 -3 parts
sort -n -o sorted +1 -2 parts
tailTAIL displays the last 10 lines of a file (if 10 not specified)
tail -30 filea
tail -50f tradinghostlog Display continuous output
tail -5 filec
whoWHO tells you who is logged on to the system
who
who am iWHO AM I identifies the current session, terminal, date and time
who am i
whoamiWHOAMI returns just the session user name
whoami
word countwc joe returns the number of lines, characters and words in the file
wc -l returns number of lines
wc -w returns number of words
wc -c returns number of characters only
C Shell commands!! - recall and execute last command
!set - repeat last set command
!echo - recall and execute last echo command
!-3 - recall and execute the third last command
!7 - recall and execute event number 7 (from the history events line)
!history - recall and execute history command
!?history - recall and execute history command from line 1 of the history events
!?joe - recall and execute joe command
!?myfile? filename - recall and myfile command containing the word filename
ls *joe - see if file joe exists
cat !$ - dispay contents of file joe (from above)
cd sass ; ls
alias del rm - creates alias of rm
alias sa "cd sass ; ls -la" - create alias ‘sa’ to change directory to sass and list files.
alias whoison "who | sort" creates alias whoison.
cd ~dave/tools/myscripts;tar cf - | (cd ~ dave/tools/newscripts; tar xvpf -) - creates a new directory and maintains the original file fingerprints/properties/permissions/owner/group and date. cp does not!
cd ~sa_vstep - returns to /home/sa_vstep
set quoted=\"quotes\" - to produce a string that retains the quotes
echo $quoted - returns "quotes"
set noquoted="noquotes"
echo $unquoted - returns noquotes
echo '*' or echo \ * - returns just an * (asterisk)
echo 'Surprise\!' - returns Surprise!
Filtersare an important unix utility for manipulating text files. They are characterised by taking input from a file, carrying out an intermediate change to the data and sending their output to a file. The input and output files may be the keyboard and the screen. Standard I/O is used by default unless otherwise specified. Each filter is a command that causes a program to be executed which carries out a certain task on a named file.
wc counts words, characters and lines in a file
grep searches for a text pattern in a file
grep [<options>] pattern <file list>
grep soot test displays the lines in file test that include the word soot
grep -vc soot test returns the number of lines in file test that don't contain the word soot
the -vc option reverses the normal command
grep abcd* searches the contents of each file in the directory for the text indicated
grep -e ' [Dd]ec | [nN]ov filename - searches filename to see if either Nov or Dec exist.
grep xyz * search all files in the current directory for the string "xyz"
egrep provides variations on the facilities provided by grep
fgrep provides variations on the facilities provided by grep
nawk this filter also performs pattern matching allowing the user to specify an action that's to be carried out on each selected line. In fact it's a full programming language in its own right, with a general syntax similar to C. It's suitable for the manipulation of files that are organised in columns of text.
ls -l | nawk '{print $9 " " $5}'
displays the contents of the 3rd and 9th columns which have been extracted and displayed in reverse order.
find recursively searches directory trees for files matching certain criteria
find <path> [<expression>] this expression forms a boolean test which is similar to a compound IF statement
find /export/home/mary the following are examples of available tests.
find . -type f -print | xargs grep xyz search all files in the current directory subtree for string xyz and ensure that no error occurs due to filename expansion exceeding system arguments limit.
To print the filename in the above add:
/dev/null to the end of the command.
care should be taken when using commands with exec, there can be unexpected results
find /example /new/example -exec grep -l 'where are you {} \;
search the two directories /example and /new/example for files containing the string ‘where are you’ and print the file names.
find / \( -name a.out -o -name '*.o'\) -atime +7 -exec rm {} \;
removes all files named a.out and *.o that have not been accessed for a week.
find /bin -type H -print Print the names of all files in the bin directory that are context sensitive (that is hidden directories)
find / -fsonly hfs -print Print the names of all files on this machine. (Be careful with this one)
find /dummy/dim -type f -print | xargs chmod 444 Change premissions on all regular files
find /dummy/dim -type d -print | xargs chmod 555 in a directory subtree to 444 and permissions on all directories to 555.
find / -acl '*.*+r' -print find all files that have a read bit set in any access control list entry.
find / -acl opt -print find all files that have optional access control list entries.
name filename searches for files that match the filename
mtime searches for files that match the identified modification time
type x searches for files that match the identified type (e.g. d,or p, or - )
print display on screen the full pathname of each matching file
exec execute the command identified
find /export/home/mary -name flip* -print search for files in a specified directory for filenames that begin with flip
find /export/home/mary -name france* -exec rm {} \; the sequence {} \; is added after a command that needs arguments
find /export/home/mary -name *.doc -exec lp this prints all the files in Mary's directory ending wiith .doc to the default printer
find -type d -print displays on screen the contents of all directories, starting from the root directory
find / displays the contents of all directories starting from the root directory
cut extracts fields of data from a data file
cut -f1 parts displays the first field of the file parts
paste inserts fields of data into existing files
paste parts suppliers will paste the contents of file suppliers to the contents of parts and display the result
removecare should be taken with the rm command
rm * will remove all files from the current directory (except the hidden files) -
rm db* deletes all files starting with db
uniquniq reports on the repeated lines in a file and removes the duplicates
uniq rhyme rhyme1will remove and report duplicates in file rhyme
translatetr translates characters in an input file - a character pattern may be searched for and replaced (translated) in a specified text file
tr [a-z] [A-Z] < rhyme replaces lower case characters by upper case
tail displays the last few lines of a file
tail -5 rhyme displays the last five lines of file rhyme