Register
It is currently Thu Apr 24, 2014 12:18 am

Check for files existence with a given extension


All times are UTC - 6 hours


Post new topic Reply to topic  [ 5 posts ] 
Author Message
 PostPosted: Wed Aug 01, 2007 4:25 am   

Joined: Sun Jul 08, 2007 2:25 am
Posts: 4
Location: Bucharest
Can anyone suggest an approach to check for files existence using only their extension? For example, I need to check if in a given directory are files with the extension *.cls, and if yes copy all these files in another location.


Top
 Profile WWW YIM  
 PostPosted: Wed Aug 01, 2007 11:11 am   

Joined: Wed Jul 25, 2007 11:45 am
Posts: 11
bogdan wrote:
Can anyone suggest an approach to check for files existence using only their extension? For example, I need to check if in a given directory are files with the extension *.cls, and if yes copy all these files in another location.



You might want to use find: `find . -name \*.tgz -exec cp {} /path/to/new/location'

This will look for .tgz files in the current directory and copy them to path/to/new/location.
Be aware that it searches recursivly, so you would need to define the maxdepth parameter:

find . -maxdepth 1 -name \*.tgz -exec

Two examples:

Code:
find . -name \*tgz
./Desktop/network.tgz
./Desktop/git-1.5.2.4/backup-072820070107-pre-git-fvcs.tgz
./sbox/ip-allocation.tgz
./sbox/bashbot.tgz
./sbox/logger.tgz
./shell.tgz
./kqemu-0.3Alpha.tgz
./network.tgz
find: ./.cpan/build/XML-SAX-Expat-0.39: Permission denied
./ip-allocation.tgz

find . -maxdepth 1 -name \*.tgz
./shell.tgz
./kqemu-0.3Alpha.tgz
./network.tgz
./ip-allocation.tgz


Top
 Profile  
 PostPosted: Sun Aug 05, 2007 2:01 pm   

Joined: Sun Jul 08, 2007 2:25 am
Posts: 4
Location: Bucharest
Thank you for your suggestion. It works fine, but the command should look like this: find . -name \*.tgz -exec cp '{}' /path/to/new/location \;
Another question: is it possible to know when the files with the extension *.tgz (following the above example) are not found and in this case display a message?

My goal is to write a script for LaTeX class installation. Here is its source, but be aware that is not yet working:

#!/bin/bash

ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=67 # Non-root exit error.
E_WRONGARGS=65 # Bad argument error
E_FAILURE=-1
E_SUCCESS=0

O_BIB="-b"
O_BST="-t"

BASE_DIR=/usr/share/texmf/ # LaTeX base directory
CLS_DIR=tex/latex/ # directory for class installation
BIB_DIR=bibtex/bib/ # directory for *.bib file(s) installation
BST_DIR=bibtex/bst/ # directory for *.bst file(s) installation
SRC_CLS_DIR="" # source directory for *.cls or *.sty file(s)
SRC_BIB_DIR="" # source directory for *.bib file(s)
SRC_BST_DIR="" # source directory for *.bst file(s)

# Run as root
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Must be root to run this script."
exit $E_NOTROOT
fi

# Check for inputs
case "$1" in
"") # no parameter
echo "At least one parameter required !"
echo "use `basename $0` class_name"
echo "or `basename $0` $O_BIB class_name, for *.bib file(s)"
echo "or `basename $0` $O_BST class_name, for *.bib and *.bst file(s)"
exit $E_WRONGARGS ;;
"$O_BIB") # install *.bib file(s)
CLS_NAME=$2
if [ -z "$CLS_NAME" ]; then
echo "You must provide the class name"
exit $E_FAILURE
fi
read -p "What is the path to the LaTeX class you want to install ? " SRC_CLS_DIR
read -p "What is the path to the *.bib file(s) you want to install ? " SRC_BIB_DIR ;;
"$O_BST") # install *.bib and *.bst file(s)
CLS_NAME=$2
if [ -z "$CLS_NAME" ]; then
echo "You must provide the class name"
exit $E_FAILURE
fi
read -p "What is the path to the LaTeX class you want to install ? " SRC_CLS_DIR
read -p "What is the path to the *.bib and *.bst file(s) you want to install ? " SRC_BIB_DIR
SRC_BST_DIR=$SRC_BIB_DIR ;;
*) # install class
CLS_NAME=$1
read -p "What is the path to the LaTeX class you want to install ? " SRC_CLS_DIR
esac

mkdir -p "$BASE_DIR""$CLS_DIR""$CLS_NAME"

find "$SRC_CLS_DIR" -maxdepth 1 -name \*.cls -exec cp '{}' "$BASE_DIR""$CLS_DIR""$CLS_NAME" \;
if [ $? ]; then
echo "*.cls file(s) installed."
fi

find "$SRC_CLS_DIR" -maxdepth 1 -name \*.sty -exec cp '{}' "$BASE_DIR""$CLS_DIR""$CLS_NAME" \;
if [ $? ]; then
echo "*.sty file(s) installed."
fi

if [ -n "$SRC_BIB_DIR" ]; then
mkdir -p "$BASE_DIR""$BIB_DIR""$CLS_NAME"
cp "$SRC_BIB_DIR"/*.bib "$BASE_DIR""$BIB_DIR""$CLS_NAME"
echo "*.bib file(s) installed."
fi

if [ -n "$SRC_BST_DIR" ]; then
mkdir -p "$BASE_DIR""$BST_DIR""$CLS_NAME"
cp "$SRC_BST_DIR"/*.bst "$BASE_DIR""$BST_DIR""$CLS_NAME"
echo "*.bst file(s) installed."
fi

echo "Updating database ..."
texhash

exit $E_SUCCESS


Top
 Profile WWW YIM  
 PostPosted: Thu Aug 09, 2007 1:27 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Disclaimer: I didn't put a lot of thought into this, so it's a dirty hack... but it should give you a general idea...

If you want error checking (such as telling you if nothing is found) you may want to use ls to build your list:

Code:
tmpfile="/tmp/file.list"
mypath="/path/to/location"

ls $mypath/*.tgz > $tmpfile 2> /dev/null

if [ -z $(cat $tmpfile) ]; then
  echo "no files found... or something"
else
  for i in `cat $tmpfile`; do
    cp $i /path/to/new/location
  done
fi

rm $tmpfile
 


There are lots of ways to do it... this is probably one of the less elegant ones...

EDIT: come to think of it, you could also just use find and pipe the output to a file instead of running exec in the find command.


Top
 Profile YIM  
 PostPosted: Sat Aug 11, 2007 3:01 am   

Joined: Sun Jul 08, 2007 2:25 am
Posts: 4
Location: Bucharest
Here is my script for LaTeX class installation. Any suggestions are welcomed:

#!/bin/bash

ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=67 # Non-root exit error.
E_WRONGARGS=65 # Bad argument error
E_SUCCESS=0

BASE_DIR=/usr/share/texmf/ # LaTeX base directory
CLS_DIR=tex/latex/ # directory for class installation
BIB_DIR=bibtex/bib/ # directory for *.bib file(s) installation
BST_DIR=bibtex/bst/ # directory for *.bst file(s) installation
SRC_CLS_DIR="" # source directory for file(s) to install

# Run as root
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Must be root to run this script."
exit $E_NOTROOT
fi

# Check for inputs
case "$1" in
"") # no parameter
echo "One parameter required! Use: `basename $0` class_name"
exit $E_WRONGARGS ;;
*) # get class name and ask for directory to look in for file(s) to install
CLS_NAME=$1
read -p "Path to the LaTeX class you want to install ? " SRC_CLS_DIR
esac

#install *.cls files if any (search in the current directory)
find "$SRC_CLS_DIR" -maxdepth 1 -name \*.cls -exec mkdir -p "$BASE_DIR""$CLS_DIR""$CLS_NAME" \; -exec cp -v -u '{}' "$BASE_DIR""$CLS_DIR""$CLS_NAME" \;

#install *.sty files if any (search in the current directory)
find "$SRC_CLS_DIR" -maxdepth 1 -name \*.sty -exec mkdir -p "$BASE_DIR""$CLS_DIR""$CLS_NAME" \; -exec cp -v -u '{}' "$BASE_DIR""$CLS_DIR""$CLS_NAME" \;

#install *.bib files if any (search in the current directory and in the directory below)
find "$SRC_CLS_DIR" -maxdepth 2 -name \*.bib -exec mkdir -p "$BASE_DIR""$BIB_DIR""$CLS_NAME" \; -exec cp -v -u '{}' "$BASE_DIR""$BIB_DIR""$CLS_NAME" \;

#install *.bst files if any (search in the current directory and in the directory below)
find "$SRC_CLS_DIR" -maxdepth 2 -name \*.bst -exec mkdir -p "$BASE_DIR""$BST_DIR""$CLS_NAME" \; -exec cp -v -u '{}' "$BASE_DIR""$BST_DIR""$CLS_NAME" \;

#update database
texhash

exit $E_SUCCESS


Top
 Profile WWW YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP