POKE ME for any consultancy

Saturday, October 18, 2014

Locating Files by “find” Command

The find command is used to locate files on a Unix or Linux system.  find will search any set of directories you specify for files that match the supplied search criteria.  You can search for files by name, owner, group, type, permissions, date, and other criteria.  The search is recursive in that it will search all subdirectories too. 

 The syntax looks like this:  find where-to-look criteria what-to-do


You can also find files with various permissions set.  “‑perm /permissions” means to find files with any of the specified permissions on, “‑perm -permissions” means to find files with all of the specified permissions on, and “‑perm permissions” means to find files with exactly permissions.  Permissions can be specified either symbolically (preferred) or with an octal number.  
The following will locate files that are writable by “others” (including symlinks, which should be writable by all):          find . -perm -o=w



A common request is a way to find all the hard links to some file.  Using “ls ‑li file” will tell you how many hard links the file has, and the inode number.  You can locate all pathnames to this file with:
  find mount-point -xdev -inum inode-number


When specifying time with find options such as ‑mmin (minutes) or ‑mtime (24 hour periods, starting from now), you can specify a number “n” to mean exactly n, “‑n” to mean less than n, and “+n” to mean more than n.
Fractional 24-hour periods are truncated!  That means that “find ‑mtime +1” says to match files modified two or more days ago.
For example:
find . -mtime 0   # find files modified between now and 1 day ago
                  # (i.e., within the past 24 hours)
find . -mtime -1  # find files modified less than 1 day ago
                  # (i.e., within the past 24 hours, as before)
find . -mtime 1   # find files modified between 24 and 48 hours ago
find . -mtime +1  # find files modified more than 48 hours ago

find . -mmin +5 -mmin -10 # find files modified between
                          # 6 and 9 minutes ago

Responsibilities of the Linux Admin

Main Responsibilities of the Linux System Administrator

  1. Maintaining User Accounts
  2. Security
  3. Managing the disk space and Backup
The Administrator’s Privileges:
  1. Change the contents or attributes of any file like its permission and ownership
  2. He can delete any file with rm even if it is write-protected.
  3. Initiate or Kill Any Process
  4. Change any user’s password without knowing the existing one
  5. Set the system clock with date
  6. Limit the maximum size of files that users are permitted to create with ulimlit
  7. Controls users access to the scheduling services like at and cron
  8. Control users access to many networking services like Telnet, FTP, etc
Commands:
# su  :- Acquire  Superuser after entering password but home directory does not change
date – Setting the system Date         # date MMDDhhmm
wall: Communicating with users  wall: Communicating with users
# wall
   The machine will be shutdown today at 14 30
   The Back up will be at 12:30  [Ctrl-d]   
ulimit: Setting Limits on File Size    ulimit: Setting Limits on File Size
passwd – Changing any Password    # passwd amit
  1. Most administrative commands are resident in /sbin and /usr/sbin
  2. Linux uses Bash for normal and system administrative operations. Using Korn or C shell, all command may not work.
  3. # in prompt indicates that the login is from root user.

input/output for C-shell (See man csh)

|pipe output to input
>redirect output to a storage file
<redirect input from a storage file
>>append redirected output to a storage file
teecopy input to both file and next program in pipe
scriptmake file record of all terminal activity

Email and communication

msgsread system bulletin board messages
mailxsend/read email; can be run by other programs to send exisiting files via email

Editors and formatting utilities

sedprogrammable text editor for data streams
vifull-featured editor for character terminals
emacsGNU emacs editor for character terminals
xemacsGNU emacs editor for X Window terminals
picovery simple text editor

Terminal control characters for C-shell (csh)

^h, backspaceerase previously typed character
^uerase entire line of input so far typed
^dend-of-input for programs reading from terminal
^ssuspend writing to terminal (freezes terminal)
^qcontinue writing to terminal
^zsuspend currently running job; restart with bg or fg
^ckill currently running program and allow clean-up before exiting
^\emergency kill of currently running program with no chance of cleanup

Information

dateshow date and time
historylist of previously executed commands
manshow online documentation by program name
infoonline documentation for GNU programs
w, whowho is on the system and what they are doing
whoamiwho is logged onto this terminal
topshow system stats and top CPU using processes
uptimeshow one line summary of system status

File management

catcombine files
cpcopy files
lslist files in a directory and their attributes
mvchange file name or directory location
rmremove files
lncreate another link (name) to a file
chmodset file permissions
cryptencode/decode a file with a private key
gzip, gunzipcompress/decompress a file
findfind files that match specific criteria

Display contents of files

catcopy files to display device
moreshow text file on display terminal with paging control
headshow first few lines of a file(s)
tailshow last few lines of a file; or reverse line order
vifull-featured screen editor for modifying text files
picosimple screen editor for modifying text files
grepdisplay lines that match a pattern
lprsend file to printer
prformat file with page headers, multiple columns, etc.
diffcompare two files and show differences
cmpcompare two binary files and report if different
commcompare two files; show common or unique lines
oddisplay binary files as eqivalent octal/hex codes
stringsshow printable text embedded in binary files
fileexamine file(s) and guess type: text, data, program, etc.
wccount characters, words, and lines in a file

Controlling program execution for C-shell (See man csh)

&run job in background
^ckill job in foreground
^zsuspend job in foreground
fgrestart suspended job in foreground
bgrun suspended job in background
;delimit commands on same line
()group commands on same line
!re-run earlier commands from history list
jobslist current jobs
psshow process information
killkill background job or previous process
nicerun program at lower priority
atrun program at a later time
crontabrun program at specified intervals
limitsee or set resource limits for programs
aliascreate alias name for program (normally used in .login file)
sh, cshexecute command file

Interpreted languages and data manipulation utilities

sedprogrammable text editor for data streams
awkpattern scanning and processing language
perlPractical Extraction and Report Language
sortsort or merge lines in a file(s) by specified fields
trtranslate characters
cutcut out columns from a file
pastepaste columns into a file
ddcopy data between devices; reblock; convert EBCDIC

Networking/communications

sshremote login/command execution; encrypted
scpremote non-interactive file copy; encrypted
sftpremote interactive file copy; encrypted
telnetremote network login; plain text password - not recommended
ftpnetwork file transfer program; plain text password - not recommended
hostfind IP address for given host name, or vice versa
lynxweb browser for character based (text-only) terminals
gzip, gunzipcompress/decompress a file
tarcombine multiple files/dirs into single archive

VI/VIM editor Commands

  • what will happen ?
                             "man ls |col -b > " will give the text file same as man ls.
  • what is VI-editor ?
      While in vi you can run AIX commands without exiting the editing session. The! creates a     shell to execute the command that follows.     1. :!ls will create a shell     2. All files in the current directory are listed. Press return to exit the shell and return to the     vi session or...     3. While still in command mode, issue the :r snacks command     4. The contents of snacks, in this case, are read into the vi file. By default, it will appear     after the current line.     If you need to run a series of commands without returning to vi after the first command is     executed, enter :sh. When you have run all the commands, press to exit the shell     and return to vi.
  • VI Options:-
                       vi has many modes of operation. Some of these will affect the way text is presented, while others will make editing easier for novice users.     :set all display all settings     :set display settings different than the default     :set ai sets autoindent on     :set noai turns autoindent mode off     :set nu enables line numbers     :set nonu turns line numbers off     :set list displays non-printable characters     :set nolist hides non-printable characters     :set showmode shows the current mode of operation     :set noshowmode hides mode of operation     :set ts=4 sets tabs to 4-character jumps     :set ic ignores case sensitivity     :set noic case sensitive
  • Search
    /word     Search “word” from top to bottom     ?word     Search “word” from bottom to top     /jo[ha]n     Search “john” or “joan”     /\< the     Search “the”, “theatre” or “then”     /the\>     Search “the” or “breathe”     /\     Search “the”     /\     Search all words of 4 letters     /\/     Search “fred” but not “alfred” or “frederick”     /fred\|joe     Search “fred” or “joe”     /\     Search exactly 4 digits     /^\n\{3}     Find 3 empty lines     :bufdo /searchstr/     Search in all open files
  • Replace
    :%s/old/new/g     Replace all occurences of “old” by “new” in file     :%s/old/new/gw     Replace all occurences with confirmation     :2,35s/old/new/g     Replace all occurences between lines 2 and 35     :5,$s/old/new/g     Replace all occurences from line 5 to EOF     :%s/^/hello/g     Replace the begining of each line by “hello”     :%s/$/Harry/g     Replace the end of each line by “Harry”     :%s/onward/forward/gi     Replace “onward” by “forward” , case unsensitive     :%s/ *$//g     Delete all white spaces     :g/string/d     Delete all lines containing “string”     :v/string/d     Delete all lines containing which didn't contain “string”     :s/Bill/Steve/     Replace the first occurence of “Bill” by “Steve” in current line     :s/Bill/Steve/g     Replace “Bill” by “Steve” in current line     :%s/Bill/Steve/g     Replace “Bill” by “Steve” in all the file     :%s/\r//g     Delete DOS carriage returns (^M)     :%s/\r/\r/g     Transform DOS carriage returns in returns     :%s#]\+>##g     Delete HTML tags but keeps text     :%s/^\(.*\)\n\1$/\1/     Delete lines which appears twice     Ctrl+a     Increment number under the cursor     Ctrl+x     Decrement number under cursor     ggVGg?     Change text to Rot13
  • Case
    Vu     Lowercase line     VU     Uppercase line     g~~     Invert case     vEU     Switch word to uppercase     vE~     Modify word case     ggguG     Set all text to lowercase     :set ignorecase     Ignore case in searches     :set smartcase     Ignore case in searches excepted if an uppercase letter is used     :%s/\<./\u&/g     Sets first letter of each word to uppercase     :%s/\<./\l&/g     Sets first letter of each word to lowercase     :%s/.*/\u&     Sets first letter of each line to uppercase     :%s/.*/\l&     Sets first letter of each line to lowercase
  • Read/Write files
    :1,10 w outfile     Saves lines 1 to 10 in outfile     :1,10 w >> outfile     Appends lines 1 to 10 to outfile     :r infile     Insert the content of infile     :23r infile     Insert the content of infile under line 23
  • File explorer
    :e .     Open integrated file explorer     :Sex     Split window and open integrated file explorer     :browse e     Graphical file explorer     :ls     List buffers     :cd ..     Move to parent directory     :args     List files     :args *.php     Open file list     :grep expression *.php     Returns a list of .php files contening expression     gf     Open file name under cursor
  • Interact with Unix
    :!pwd     Execute the “pwd” unix command, then returns to Vi     !!pwd     Execute the “pwd” unix command and insert output in file     :sh     Temporary returns to Unix     $exit     Retourns to Vi
  • Alignment
    :%!fmt     Align all lines     !}fmt     Align all lines at the current position     5!!fmt     Align the next 5 lines
  • Tabs
    :tabnew     Creates a new tab     gt     Show next tab     :tabfirst     Show first tab     :tablast     Show last tab     :tabm n(position)     Rearrange tabs     :tabdo %s/foo/bar/g     Execute a command in all tabs     :tab ball     Puts all open files in tabs
  • Window spliting
    :e filename     Edit filename in current window     :split filename     Split the window and open filename     ctrl-w up arrow     Puts cursor in top window     ctrl-w ctrl-w     Puts cursor in next window     ctrl-w_     Maximise current window     ctrl-w=     Gives the same size to all windows     10 ctrl-w+     Add 10 lines to current window     :vsplit file     Split window vertically     :sview file     Same as :split in readonly mode     :hide     Close current window     :only     Close all windows, excepted current     :b 2     Open #2 in this window
  • Auto-completion
    Ctrl+n Ctrl+p (in insert mode)     Complete word     Ctrl+x Ctrl+l     Complete line     :set dictionary=dict     Define dict as a dictionnary     Ctrl+x Ctrl+k     Complete with dictionnary
  • Marks
    mk     Marks current position as k     ‘k     Moves cursor to mark k     d’k     Delete all until mark k Abbreviations     :ab pr printf("This is a Demo Ver \n");    Define pr as abbreviation of printf("This is a Demo Ver \n");
  • Text indent
    :set autoindent     Turn on auto-indent     :set smartindent     Turn on intelligent auto-indent     :set shiftwidth=4     Defines 4 spaces as indent size     ctrl-t, ctrl-d     Indent/un-indent in insert mode     >>     Indent     <<     Un-indent
  • Syntax highlighting
    :syntax on     Turn on syntax highlighting     :syntax off     Turn off syntax highlighting     :set syntax=perl     Force syntax highlighting    
  • How to Exit
    :q[uit]     Quit Vim. This fails when changes have been made.     :q[uit]!     Quit without writing.     :cq[uit]     Quit always, without writing.     :wq     Write the current file and exit.     :wq!     Write the current file and exit always.     :wq {file}     Write to {file}. Exit if not editing the last     :wq! {file}     Write to {file} and exit always.     :[range]wq[!]     [file] Same as above, but only write the lines in [range].     ZZ     Write current file, if modified, and exit.     ZQ     Quit current file and exit (same as ":q!").
  • Editing a File
    :e[dit]     Edit the current file. This is useful to re-edit the current file, when it has been changed outside                 of Vim.     :e[dit]!     Edit the current file always. Discard any changes to the current buffer. This is useful if you want                 to start all over again.     :e[dit] {file}     Edit {file}.     :e[dit]! {file}     Edit {file} always. Discard any changes to the current buffer.     gf     Edit the file whose name is under or after the cursor. Mnemonic: "goto file".
  • Inserting Text
    a     Append text after the cursor [count] times.     A     Append text at the end of the line [count] times.     i     Insert text before the cursor [count] times.     I     Insert text before the first non-blank in the line [count] times.     gI     Insert text in column 1 [count] times.     o     Begin a new line below the cursor and insert text, repeat [count] times.     O     Begin a new line above the cursor and insert text, repeat [count] times.     
  • Inserting a file
    :r[ead] [name]     Insert the file [name] below the cursor.     :r[ead] !{cmd}     Execute {cmd} and insert its standard output below the cursor.