Linux and Unix - These apply to most Unixes (Solaris, AIX, HP-UX and even cygwin)
Linux/Unix snippets and tips:
This diff's each file in two directories (as opposed to just diff'ing the directories ls - l creates a list from one dir, nawk then converts this to a two column file list n in xargs specifies the number of args to read at once
ls -l ../src/ | nawk '/^d/ { print $9 " ../src/" $9 }' | xargs -n 2 diff $1 $2
This is an alternative to the above tell xargs to use one line at once - seems nicer
ls -l ../src/ | nawk '/^d/ { print $9 " ../src/" $9 }' | xargs -l diff $1 $2
build directory structure
ls /some/dir | grep ".*/" | xargs mkdir
copy files from subdirs of parent dir to this dir (must have subdirs in place)
find .. -name "*.F" | sed 's/..//' | xargs -I '{}' -t cp ../'{}' ./'{}'
tar selected files
tar cvf ../shmuser/src.tar */{*.{F},*.{h},Make*.*}
Count files in a directory - use wc (word count) which reports lines, words and letters use -l to get line count
ls f* | wc -l
Create a tar based on find
find . -type f -name "*.java" | xargs tar rvf myfile.tar
Shell script to remove files of 0 length handy if a redirect yeilds no output
# Compare the file results
files="file1 file2"
for file in $files
do
#cmp does binary file compare
cmp dir1/${file} dir2/${file} > $file.dif
if [ ! -s $file.dif ]
then
rm -f $file.dif
fi
if [ $? -ne 0 ]
then
echo "Differences found in" ${file}.dat
else
echo "No differences found in" ${file}.dat
fi
done
Awk/Shell script find files of given length
FS9=`ls -l fort.9 |awk '{print $5}'`
if [ $FS8 -eq 0 ] || [ $FS9 -gt 100 ]
then
...
fi
Get the absolute version of a relative path
ABS_DIR=`(cd ../../../..; pwd)`
Change case of filenames
for i in `ls`
do cp $i `echo $i | tr '[:upper:]' '[:lower:]'`;
done
Split a file into parts
csplit -k big-file '/regexp'/ '{100}'
Note the ' ' around the {} are NEEDED in CSH as CSH would otherwise interpret the {} before it got to csplit. A prefix and number of digits on the split files can also be provided
Produce a tree listing for a directory structure
If you are on a Unix system that doesn't have a tree list then
try this//
Credit to the origiantor listed in the header
[~ <pre class="brush: bash">
- A better tree, based on tree script from #
- http://www.centerkey.com/tree #
- #
echo
if [ "$1" != "" ] #if parameter exists, use as base folder
then cd $1
fi
pwd
ls -R | grep ":$" | #following may need to be all on one line
sed -e 's/:$//' | awk -F/
' { printf "|"; for (i=1; i<=NF-2; i++) { printf " ", $i; }
{if(NF>2)printf " +- ";else printf "--"} print $NF }'
if [ `ls -F -1 | grep "/" | wc -l` = 0 ] # check if no folders then echo " -> no sub-directories" fi echo exit </pre>
csh
If you are unfortunate to end up using csh (CShell) then here are a few things to make life a bit easier - to set a shortened version of your path as the prompt
Create an awk file containing:pwd | awk -F/ '{ if(NF < 4 )print ; else {for (i=NF-3; i<=NF; i++) printf "%s/", $i}; }'
and aliases foralias setprompt 'set prompt="`whoami`@`uname -n`:`~/shortPathPrompt.awk`> "'
alias cd 'chdir \!* && setprompt'
Turn on filename completion with
set filec
use Esc (not tab)
to complete a file/dirctory or Ctrl+D to list available completions
- Login to post comments