1 / 16

find Command

Learn to locate files, customize prompts, manage disk usage, create aliases, variables, scripts, and perform command substitution. Understand the $PATH variable, shell scripts, script arguments, decisions, loops, and functions effectively. Dive into examples and enhance your Unix skills today!

fjeff
Download Presentation

find Command

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. find Command • Characteristics • Locate files descending from multiple starting points • Employs regular expressions • Examples On entire system: >find / -name "cs*" Two starting points: >find /usr /var/html –name "cs*" Case insensitive: >find /usr –iname "*htm*" More than 3 days ago: >find /usr -mtime +3 –name "*htm*" Within the last 3 days: >find /usr –mtime -3 –name "*htm*"

  2. Customizing your prompt • To set: set $PS1 variable with a prompt string. • Additional prompt string capabilities:\d = todays date\H = host name\T = current time\w = working directory\! = history number of this command • Example: export PS1=">" • Example: export PS1="\! \w >"

  3. Disk Usage Commands • Free Blocks: >df • Disk usage • Size of each directory (recursive): >du • Summary of each directory (not recursive):>du –s * • Size of a single directory: >du –s <fileName> • Sort usage, numeric reverse, page at a time>du | sort –nr | more

  4. Aliases Replace a word by a string when starting a command • Definition: Replace a string by a word at the start of a command • Use: Shorten what needs to be typed by aliasing a command with its arguments • Use: Rename a command to something more familiar • Examples • Display all of the aliases in use: >alias • Example (dos user directory list): >alias dir='ls –l' • To remove an alias: >unalias dir

  5. Creating Script Variables • create a variable: <var>="value" • Note: do not use spaces • Example: >TEAM="Yanks" >echo $TEAM • Variables are case sensitive • You can use variables anywhere in commnads >bindir=~bin >echo $bindir • To make variables accessible to all sub shells, use export: >export TEAM="yanks"

  6. Command Substitution The output of one command becomes part of another • Setting variables: `<cmd>` or $(<cmd>) • Examples • >dir=`pwd`>echo $dir • files=$(ls)echo $files • echo there are `grep –ci bush grades` bush\'s • From command line: `<cmd>`echo `date`grep "abc" `cat file.txt` Note: echo “$(ls)” preserves the new lines in the output Note: The back quote is found on the top left of most keyboards

  7. $PATH Environment Variable • Most unix commands are C programs • When you execute a command • $PATH contains a series of path names • The shell searches all variables in $PATH • To add program/bin to the $PATH variableexport PATH = $PATH:program/bin • To add the current directory to $PATHexport PATH = $PATH:.

  8. Shell Scripts See Bash's Beginners guide See examples on the class web site • Put multiple commands in a file and then execute them by typing the file name • They execute the statements one by one • Creating a script • The first line should be #!/bin/bash • Other lines starting with # are comments • Make executable: u+x scriptName • Execute: ./scriptName [unless working directory is in the search path] • Use script capabilities to • create loops • create decisions • write functions

  9. Arguments to a Shell Script • Arguments to a script • $0 is the name of the shell script (argument 0) • $1 is the first argument • $2 is the second argument • $k is the kth argument • $# is the number of arguments • $@ is all of the arguments • Example: scriptFile x y z • $0 = scriptFile • $2 = y • $# is 3 • $@ is x y z

  10. Shell Script Decisions if [ condition ]; then <statements> fi if [ condition ]; then <statements> else fi if [ $1 == 3 ] ; then echo "arg equals 3" else echo "arg not 3"; fi Note: space after [ and before ], semi colon after ] Comparators: ! -lt –le –gt –ge –f == -r where -f is file –r for readable file

  11. while and do Loops while [ condition ]; do command1; command2; command3 done i=0 while [ $i –lt 4 ]; do echo "Welcome $i"; i=$[$i+1]; done or i=0 do until [ $i == 4 ]; do echo "Welcome $i"; i=$[$i+1]; done or i=0 while [ $i –lt 4 ]; do echo "Welcome $i" i=$[$i+1] done Semicolons separate parts of the loop on the same line Boolean variables true or false are legal

  12. for Loop for <varName> in <list of words>;do <statements>; done; Example: for i in `cat list`; do cp "$i" "$i.bak;" done Example: for i in $PATHNAME/*; do rm $PATHNAME; done Example: >for x in *; do echo $x; done Note: the last example is directly from the command line

  13. Calculate the average in a script #!/bin/bash if [ $# -lt 1 ]; then echo "usage: at least one argument needed" exit 1 fi value=0 count=0 for i in $@ do value=$[$value+$i] count=$[$count+1] done average=$((value/count)) echo $average average 1 outputs 1 average 1 2 3 outputs 2

  14. Script Functions <funcName>() { <cmdList> } Example: doSomething doSomething() { ls –l | more; } doSomething Note:Function must be declared before it can be used. Note:Spaces required before and after braces Note: A common error is forgetting the required spaces

  15. Functions with arguments • Arguments • $@ means all arguments • $* means all arguments, but treats quoted argments as one with embedded spaces • $0 is the command name • $1 first argument • $2 second argument • $# number of arguments • Example: LL() { ls –lR $@ | more; } LL / • Example: mycd() { cd $@; pwd; }

  16. Shell Startups .bash_profile and .bashrc in a user’s home directory • .bash_profile • configures the login environment • Most .bash_profiles also execute .bashrc • .bashrc: configures subshell environments • To customize a users environment, normally edit .bashrc • include • Include aliases, variables, functions, search path ($PATH) • Good idea: Make the script in a test file first • Takes effect when logging in or executiong the source command

More Related