Advanced Shell Operations
From Free Knowledge Base- The DUCK Project: information for everyone
. @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ . @@ Example shell scripts useful for many tasks and provided by the community @@ . @@ Sat Jun 2 12:22:22 CDT 2001 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ . @@ Guide to Advanced Shell Operations @@@@@ shell script examples @@@@@@@@@@@@@ . @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ . . @ discard the output of a command @ . . command > dev/null # eliminates output of STDOUT . . command > /dev/null 2>&1 # redirects STDERR to STDOUT and . eliminates them . . @ use the value of a shell variable in a sed command @ . . sed "/$DEL/d" file1 > file2 # deletes lines in file1 containing . the value of $DEL . . @ check to see whether a variable has a value @ . . if [ -z "$VAR" ] ; then list ; fi # list is the command that executes if . $VAR doesn't have a value . . @ determine the full pathname of a directory @ . . FULLPATH=`(cd dir ; pwd)` # determine full path of directory . . @ determine the full pathname of a file @ . . CURDIR=`pwd` # save current directory path . cd `dirname file` # change to directory with the file . FULLPATH="`pwd`/`basename file`" # join output of pwd and filename . cd $CURDIR # go back to the original directory . . @ rename all the *.html files to *.php in a directory @ . . OLDSUFFIX=html # variable with old suffix . NEWSUFFIX=php # variable with new suffix . for FILE in *."$OLDSUFFIX" # all files that start with old suffix . do . NEWNAME=`echo "$FILE" | sed -e "s/${OLDSUFFIX}\$/$NEWSUFFIX/"` . mv "$FILE" "$NEWFILE" # sed does a replace and then mv . done # to move the files . . @ rename all the *.html files to *.htm using bash pattern matching @ . . for i in *.html; do . if [ -f ${i%l} ]; then . echo ${i%l} already exists . else . mv $i ${i%l} . fi . done . . @ rename all default* files to index* @ . . OLDPREFIX=default # variables with old and new prefix . NEWPREFIX=index # for loop for all files with prefix . for FILE in "$OLDPREFIX"* # sed -e for script, s for substitute . do # carrot indicates start of line . NEWNAME=`echo "$FILE" | sed -e "s/^${OLDPREFIX}/$NEWPREFIX/"` . mv "$FILE" "$NEWNAME" . done # rename the file and exit loop . . @ rename variations, example removes Shortcut to from beginning of files @ . . OLD="Shortcut to "; NEW=""; for FILE in "$OLD"*; do; NEWNAME=`echo "$FILE" | sed -e "s/^${OLD}/$NEW/"`; mv "$FILE" "$NEWNAME"; . # places everything from above script . on a single line, does a substitute . . OLD="Shortcut to "; for FILE in "$OLD"*; do NEWNAME=`echo "$FILE" | sed -e "s/^${OLD}//"`; mv "$FILE" "$NEWNAME"; echo "$NEWNAM . # since it is not a substitute, this . uses 1 variable but still echos each . file as it is processed . . OLD="Shortcut to "; for FILE in "$OLD"*; do NEWNAME=`echo "$FILE" | sed -e "s/^${OLD}//"`; mv "$FILE" "$NEWNAME"; done . # eliminate unecessary echo of filename . . for FILE in "Shortcut to "*; do N=`echo "$FILE" | sed -e "s/^Shortcut\ to\ //"`; mv "$FILE" "$N"; done . # shortest method, eliminates use of . variables and filename echo . . @ set all filenames in a directory to lowercase @ . . for FILE in * # use mv -i to avoid overwriting files . do # uses the tr command to convert case . mv -i "$FILE" `echo "$FILE" | tr '[A-Z]' '[a-z]'` 2> /dev/null . done . . @ eliminate msdos carriage returns in files @ . . # dos uses \r\n (^M) where UNIX uses . only \n . tr -d '\015' < file1 > file2 # file1 contains carriage returns, file2 . is created without carriage returns . # the \015 is oct representation of dos . carriage returns . . -new entries- . . @ search and replace a text string with another in multiple files @ . . for e in *; do mv $e $e.tmp ; sed 's/STRING1/STRING2/g' <$e.tmp >$e ; rm -f $e.tmp ; done . . (works best in a directory without subdirectories; it does bad things to subdirectories) . . @ replace a text string with another in files and recurse into directories @ . . for e in `find . -type f`; do mv $e $e.tmp ; sed 's/STRING1/STRING2/g' <$e.tmp >$e ; rm -f $e.tmp ; done . . @ test to see if a file exists and print true if it does . . if test -e /tmp/seclog.pid ; then echo "true"; fi . . -lazygirl 4/29/2002 . . @ remove text from within filenames in a directory of files. @ . . All these files have a text string in the middle of their filename. The text we want to remove from . within the file is: WEB.x264-video. . so the file lexS01E01.WEB.x264-video.avi needs to change to lexS01E01.avi and the periods need escaped out . . for file in *; do mv "${file}" "${file/WEB\.x264-video\./}"; done . . @ replace linux line feeds in file containing a LF delimited list with a comma . and space single line list. (file had space between last char and LF) @ . . sed 's/.$/,\ /g' < parseme.txt > tmpfile.txt; tr -d '\012' < tmpfile.txt > outfile.txt . . -lazygirl 8/23/2004 . . @ compare IP address to C block and if it is part of that network @ . . host24=${ipaddress%.*} . if [ "${host24}" = "192.168.30" ]; then . echo "${ipaddress}"; . fi . . -Sun Nov 14 15:30:55 CST 2004 . . @ useful 'c' like FOR loop (for/next) for bash @ . . for i in `seq 0 99`; . do . echo number $i . done . . -krissyj Dec 2004 . . @ unzip a directory of zip files all at once @ . . for FILE in *.zip; do unzip "$FILE"; done . . @ convert list of usernames into delete user shell script @ . . :1,$s/^\(.*\)/userdel -r \1/ . . @ live active monitoring of tcp connections from the shell @ . . while $x <> 1; do . clear . echo "Current TCP connections for robotz.com" . netstat -np|grep "tcp\ "; . sleep 15s . done . . @ old fashioned replace text string for every file in directory @ . . change string1 to string2 in every file within the directory . . for e in *; do sed 's/<string1>/<string2>/g' $e >$e.tmp; rm -f $e; mv $e.tmp $e; done . . @ strip HTML tags out of a text/html file. This works unless a tag is split @ . and ends on the following line, such as long a href tags . . e=index.html; sed 's/<[^>]*>//g' $e >$e.tmp; mv $e.tmp $e . . @ prepend text- to the beginning of all JPG files in a directory @ . . for f in *.JPG; do mv "$f" "text-$f"; done
Related Pages
If what you're looking for isn't on this page, try some of these related pages:
- Console Command Examples
- Console Command Reference
- Advanced Shell Operations
- How Do I: A Linux Q&A
Carbonite Page: This page is an archived entry and should not be modified. Information on this page may be out of date, however, still useful. An example is data for legacy systems or outdated technologies that are still being used by some, such as Vacuum Tube bogey values, analog broadcast television frequencies or MS-DOS 6.22 commands. Although outdated, this information will not change or evolve any further. Certain archived pages on the D.U.C.K. Wiki have been imported from our older ANSI BBS system and other storage mediums previously used. These pages are kept unmodified for archival purposes. |