Translate

Friday 31 January 2014

OTHER FILE HANDLING COMMANDS


type <command>
- show where the source of a command is: e.g.
$ type sendmail
sendmail is /usr/sbin/sendmail

This command is merely an alias for 'whence -v'
whence <command>
- show where the source of a command is: shell builtin command. See type
Use option: -v for verbose mode
which <command>
- show where the source of a command is held. Almost the same as type and whence
chmod <Octal Permissions> <file(s)>
- change file permissions. e.g.
$ chmod 666 handycommands
changes the permissions (seen by ls -l) of the file handycommands to -rw-rw-rw-
r = 4, w = 2, x = 1. In the above example if we wanted read and write permission for a particular file then we would use r + w = 6. If we then wanted to have the file have read-write permissions for User, Group and All, then we would have permissions of 666. Therefore the command to change is that above.
$ chmod 711 a.out
Changes permissions to: -rwx--x--x
Additional explanation of file permissions and user/group/all meaning are given in the description of ls -l
You may specify chmod differently - by expressing it in terms of + and - variables. For example
$ chmod u+s /usr/bin/su
will modify the "sticky bit" on su, which allows it to gain the same access on the file as the owner of it. What it means is "add s permission to user". So a file that started off with permissions of "-rwxr-xr-x" will change to "rwsr-xr-x" when the above command is executed. You may use "u" for owner permissions, "g" for group permissions and "a" for all.
chown <Login Name> <file(s)>
- Change ownership of a file. Must be done as root. e.g.
chown informix *.dat # change all files ending .dat to be owned by informix
chgrp <Group Name> <file(s)>
- Change group ownership of a file. Must be done as root. e.g.
chgrp sys /.netrc # change file /.netrc to be owned by the group sys
mvdir <Source Directory> <Destination Directory>
- move a directory - can only be done within a volume group. To move a directory between volume groups you need to use mv -r
or find <dirname> -print | cpio -pdumv <dirname2>rm -r <dirname>
cpdir <Source Directory> <Destination Directory>
- copy a directory. See mvdir
rmdir <Directory>
- this is crap - use rm -r instead
mkdir <Directory>
- Creates a directory. e.g.
$ mkdir /tmp/jon/ # create directory called /tmp/jon/ 
find <pathname> -name "searchkey" -print
- search for files - e.g.
$ find . -name "system.log" -print # will find all files (with full path names) called system.log - Wildcards are allowed, e.g.
$ find /tmp -name "sl.*" -atime +0 -print # will print out all files in /tmp/ that start sl. and which haven't been accessed for a day. Helpful for finding lost files, or finding stuff in enormous directories. Other useful options include:
  • -atime +<days> - finds files that haven't been accessed for 1+days also, ctime (creation time) and mtime (modify time)
  • -prune - stay in current directory - don't look in dirs off the directory specified in path names - e.g.
    $ find /tmp -user "compgnc" -prune -print # will find all files in /tmp which user compgnc owns and will not search lower directories (e.g. /tmp/usr)
  • -size +<blocks> - finds files that are bigger than <blocks>
  • -exec rm {} \; - remove all files found...dangerous command - e.g.
    $ find /tmp -name "sl.*" -atime +0 -prune -print -exec rm {} \; # will remove all files in /tmp starting 'sl.' that haven't been accessed for a day. Spacing of this command is important! Most exec commands are possible:
    $ find /usr2/calltest -name "*.4gl" -print -exec grep "CHECK" {} \; | pg
  • -ok - like exec only it prompts for confirmation after each occurence. e.g.
    $ find /tmp/disk7 -name "*" -print -ok doswrite -a {} {} \; # Please note that you MUST end any exec or ok option with an escaped semicolon (\;).
  • -user <username> - finds all files owned by <username>
  • -group <groupname> - finds all files with a group of <groupname>
ln -s <Directory> <symbolic link>
- create a symbolic link to a different directory from current directory: e.g.
$ ln -s /usr/uniplex/compgnc /u/compgnc/uni # would create a link called 'uni' in the directory /u/compgnc. From then on, typing cd uni would cd to /usr/uniplex/compgnc. You can also give two files the same name. e.g.
$ ln make.e_enquiry makefile # would link the two files so that they are identical, and when you change one, you change the other. You may also create a symbolic link to a host(!). Instead of typing 'rlogin hpserver' every time, by typing
$ ln -s /usr/bin/rsh hpserver # will create a link so that whenever you type 'hpserver' it will execute a remote shell on the machine.
Option -f forces the link to occur
head -<Number> <FileName>
- prints out the first few line of a file to screen. Specify number to indicate how many lines (default is 10). e.g. If you sent something to a labels printer and it wasn't lined up, then you could print the first few labels again using:
$ head -45 label1.out | lp -dlocal1
tail -<Number> <FileName>
- prints out the end of a file. Very similar to head but with a very useful option '-f' which allows you to follow the end of a file as it is being created.e.g.
$ tail -f vlink.log # follow end of vlink.log file as it is created.
wc -<options> <FileName>
- Word Count (wc) program. Counts the number of chars, words, and lines in a file or in a pipe. Options:
  • -l (lines)
  • -c (chars)
  • -w (words)
To find out how many files there are in a directory do ls | wc -l
split -<split> <FileName>
- Splits a file into several files.e.g.
$ split -5000 CALLS1 # will split file CALLS1 into smaller files of 5000 lines each called xaa, xab, xac, etc.
tr <character> <other character>
- translates characters. e.g.
cat handycommands | tr "\t" " " # will take the file handycommands and translate all tabs into spaces. Useful when messing about with awk or you need to convert some input (e.g. that from tty) to a unique filename that does not contain special characters. e.g.
tty | tr "/" "." # produces for example .dev.pts.7
od <options> <filename>
- od converts nasty (binary save) files into character representations. Useful when back-compiling, examining raw .dat files,etc. Use with option '-c' for character display (recommended).
script
- starts recording everything in the shell to a file by default 'typescript'. Press ^D to finish the script. Provides a log of everything used. Has almost the same effect as ksh | tee typescript
Used for debugging shells, seeing error messages which flash off the screen too quickly, etc.
cut
- cut's the file or pipe into various fields. e.g.
$ cut -d "|" -f1,2,3 active.unl # will take the file active.unl which is delimited by pipe symbols and print the first 3 fields options:
  • -d <delimiter>
  • -f <fields>
Not too useful as you can't specify the delimiter as merely white space (defaults to tab) Alternatively, you can 'cut' up files by character positioning (useful with a fixed width file). e.g.
$ cut -c8-28 "barcode.txt" # would cut columns 8 to 28 out of the barcode.txt file.
paste
- paste will join two files together horizontally rather than just tacking one on to the end of the other. e.g. If you had one file with two lines:
Name:
Employee Number:
and another file with the lines:
Fred Bloggs
E666
then by doing:
$ paste file1 file2 > file3 # this would then produce (in file3).
Name: Fred Bloggs
Employee Number: E666
Note that paste puts horizontal tabs between the files, so you may need a sed 's/   //g' command to get rid of these.
sort <filename>
- sorts the information from the file and displays the result on standard output (stdout). e.g.
$ sort /tmp/list_of_names # will sort the file into alphabetical order, and display it to the screen. Useful with option '-u' to filter out duplicates.
uniq <filename>
- filters out all duplicate lines from a file or input stream (file or stream must be sorted!). Useful with option -c which merely produces a count of unique lines.
ex <filename>
- ex is an old line editor, and almost never used now (similar to DOS edlin if you remember that - me, I've repressed it). You are most likely to come across ex within the vi editor - all commands beginning with a colon (:) are ex commands

No comments:

Post a Comment