Truncate filename using find


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Truncate filename using find
# 1  
Old 02-17-2012
Question Truncate filename using find

I have a script that executes the following code and I could use some help.
Code:
find $searchdir -maxdepth 2 -type d -name $dir -exec mkalias {} ./$dir/_$dir$suffix \;

(mkalias is an executable to create mac aliases)
(The script takes a directory name finds that same name on another drive and creates a mac alias to it inside the named directory and adds a _ to the beginning and .Link to the end.)

The problem I'm having is that the filenames created by mkalias are whacky.
Like this:
Code:
_XXX_004_010_Laptop#C93202.Link

It should read:
Code:
XXX_004_010_Laptop_Over_O_First_Look.Link

So rather than try to figure that out I think I'd rather truncate the file name at the first 11 characters so it creates:
Code:
_XXX_004_010.Link

Is there something I can add so that only the first 11 characters are used?

Last edited by methyl; 02-19-2012 at 03:53 PM..
# 2  
Old 02-17-2012
The first thing I'd do is make it echo mkalias instead of mkalias to make sure you're feeding the command what you think you are...
# 3  
Old 02-17-2012
Please post what Operating System and version you have and what Shell you prefer.

When posting script samples with unexplained variable names, please post the values of those variables. In this case $searchdir , $dir and $suffix.

If you are using a non-industry-standard term like "mac alias", please explain briefly what the term means.

Code:
# Diagnostic version of the code
echo "The value of searchdir is: ${searchdir}"
echo "The value of dir is: ${dir}"
echo "The value suffix is: ${suffix}"
find "${searchdir}" -maxdepth 2 -type d -name "${dir}" -print | while read dirname
do
            echo "Generated command is: mkalias ${dirname} ./${dir}/_${dir}${suffix}"
done


Last edited by methyl; 02-17-2012 at 07:23 PM..
# 4  
Old 02-18-2012
Thanks for the help, here's the rest of the info.

I'm using OSX 10.6.8

It's a c shell but I think bash would be better.
Mac alias is the alias files created by the OSX's finder. OsxUtils' mkalias is how I create the aliases through a command line.
sveinbjorn.org/osxutils_docs

Here's the script:

Code:
#!/bin/csh -f

if ($#argv == 0) then
echo "Renders Alias Maker"   

echo "    Usage: ram <filename>"
	echo "Renders Alias Maker"
  exit
endif

### Set input directory from command line:
set dir = $1:r

### Set search directory:
set searchdir = /Volumes/SHOWS/SAV/01_Shots/

set suffix = .Link

### Execute script:
### Original working code:
### find $searchdir -maxdepth 2 -type d -name $dir -exec mkalias {} ./$dir/_$dir$suffix \;

### NEW CODE:
find $searchdir -maxdepth 2 -type d -name $dir -exec mkalias {} ./$dir/_$dir$suffix \;

# 5  
Old 02-19-2012
Because this is "csh" on a MAC, I cannot help.
The link to a 2005 explanation of the utility is:
osxutils Documentation | Sveinbjorn Thordarson
# 6  
Old 02-20-2012
Here's the code for mkalias:
Code:
#!/bin/sh

scriptname=`basename $0`
if [ $# -lt 2 ]; then
    echo "Usage: $scriptname srcPath destPath"
    exit
fi

srcPath=$1
destPath=$2

if [ ! -e "${srcPath}" ]; then
    echo "$scriptname: '${srcPath}': No such file or directory"
    exit
fi

# remove possible trailing slash from $srcPath
srcPath="${srcPath%/}"

# set $srcType to "file" or "folder" as appropriate
if [ -d "${srcPath}" ]; then
    if [ "${srcPath##*.}" == "app" ]; then
        srcType="file"
    else
        srcType="folder"
    fi
else
    srcType="file"
fi

if [ ! -d "${destPath}" ]; then
    echo "$scriptname: '${destPath}': No such directory"
    exit
fi

case "${srcPath}" in
/*) fullSrcPath="${srcPath}" ;;
~*) fullSrcPath="${srcPath}" ;;
*)  fullSrcPath=`pwd`/"${srcPath}" ;;
esac

case "${destPath}" in
/*) fullDestPath="${destPath}" ;;
~*) fullDestPath="${destPath}" ;;
*)  fullDestPath=`pwd`/"${destPath}" ;;
esac

/usr/bin/osascript > /dev/null <<EOT
tell application "Finder"
    set macSrcPath to POSIX file "$fullSrcPath" as text
    set macDestPath to POSIX file "$fullDestPath" as text
    make new alias file to $srcType macSrcPath at folder macDestPath
end tell
EOT

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to find a filename of a folder?

Hi, Please help me i have a file directory in .txt. inside this directory is a list of names of folders to find inside in a folder. to make more easy to understand below is my list inside .txt: foldernameA foldernameB foldernameC I need to check if those folder names are inside... (1 Reply)
Discussion started by: cmarzan
1 Replies

2. Shell Programming and Scripting

Find files greater than a particular date in filename.

I need a unix command which will find all the files greater that a particular date in the file name. say for example I have files like(filenaming cov : filename.YYDDMMSSSS.txt) abc.201206015423.txt abc.201207013456.txt abc.201202011234.txt abc.201201024321.txt efg.201202011234.txt... (11 Replies)
Discussion started by: lijjumathew
11 Replies

3. Shell Programming and Scripting

Only filename from find command

Hi All When we use find command command in Unix we get the result as /home/user/folder/filename1 /home/user/folder/filename2 /home/user/folder/filename3 Is it possible that i only get the file name The expected output when using find command is filename1 filename2 filename3 I am... (13 Replies)
Discussion started by: parthmittal2007
13 Replies

4. Shell Programming and Scripting

Howto get only filename from find command in Linux?

Hi every body! I would like to get only filename in the result of find command in Linux but I don't know howto. Tks so much for your helps. (5 Replies)
Discussion started by: nguyendu0102
5 Replies

5. Shell Programming and Scripting

how to get only filename in a recursively find command

Hi i would like to ask on how to accomplish the FF: I want to execute a find command recursively and only get the filename something like i want only the last field set if is used ever the fieldvset as an redirection from the output of the find command For example: dir1/dir2/filename1... (2 Replies)
Discussion started by: jao_madn
2 Replies

6. Solaris

Find retrieves file which filename was in ! -name

Hi pals, I'm a little puzzled. I've got a script that makes a list of files this find. find . -type f ! -name $list ! -name ${list}.20 ! -name ${list}.new ! -name '.*' ! -name '*.dc' ! -newer $list.20 -newer ${list}.new -exec ls -E {} \; | grep -v './.*/' > ${list} This script is called by... (0 Replies)
Discussion started by: kl0x
0 Replies

7. Shell Programming and Scripting

Find directories that contains more than n matches of a certain filename

I need to construct a command that finds directories which contains more than n matches of a certain filename. E.g. I have many directories at different locations and want to find all directories that has 2 or more .dat-files. I thought of using find and maybe the exec parameter to issue an... (5 Replies)
Discussion started by: odyssey
5 Replies

8. Shell Programming and Scripting

Find filename in the given directory

Hi , I have question for search the filename in directory. For example my DIR contains these files... testA123.txt testB123.txt testB345.txt testA345.txt i want to show the filenames which contains 'testA7'... Help me (7 Replies)
Discussion started by: karthinvk
7 Replies

9. UNIX for Dummies Questions & Answers

help to find find filename usingFile pointer in c

Hi .. I am having a file pointer .. it have declared some where tough to find out that ..Can we find out the filename associated with file pointer ... i mean is there any function FILEPOINTER.filename() is there in c ??... Thanks, Arun (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

10. UNIX for Dummies Questions & Answers

find filename based on file content

:confused: There is a flat file on my system which contains email addreses of people in my company. This file is utilized when sending notifications for various things. However nobody knows where this file is located or what it is named. The only thing we know is the email address of a user who... (4 Replies)
Discussion started by: kollerj
4 Replies
Login or Register to Ask a Question