How to get a directory of a file?


 
Thread Tools Search this Thread
Operating Systems AIX How to get a directory of a file?
# 1  
Old 12-10-2013
How to get a directory of a file?

Hi,

I need a comamnd or script to get directory of a particular file.

TIA
# 2  
Old 12-10-2013
# 3  
Old 12-10-2013
If you are looking to do this repetitively, then you can cost a significant amount to be starting a process for each time.

You may be better using variable substitution which is all in the same process.

You don't tell us your shell, so I will use ksh for an example:-
Code:
#!/bin/ksh

fullname="/a/b/c/d/e/f"

dir_name="${fullname%/*}"
filename="${fullname##*/}"


echo "You are working from ${fullname}, so a directory of ${dir_name} and file ${filename}"

# 4  
Old 12-10-2013
If you want to know in a single command.
find / -name <filename>
Remember it will search everything and might throw some errors (especially for NFS mounted file systems), but actual result will be shown at the bottom.

I hope this helps.
# 5  
Old 12-10-2013
Quote:
Originally Posted by rbatte1
If you are looking to do this repetitively, then you can cost a significant amount to be starting a process for each time.

You may be better using variable substitution which is all in the same process.

You don't tell us your shell, so I will use ksh for an example:-
Code:
#!/bin/ksh

fullname="/a/b/c/d/e/f"

dir_name="${fullname%/*}"
filename="${fullname##*/}"


echo "You are working from ${fullname}, so a directory of ${dir_name} and file ${filename}"

$filename is always correct and a good replacement of the basename command.
But $dir_name is not correct in the cases
Code:
fullname="f"
fullname="/f"

So there is a good reason for the dirname command.

---------- Post updated at 11:33 AM ---------- Previous update was at 11:30 AM ----------

Quote:
Originally Posted by ibmtech
If you want to know in a single command.
find / -name <filename>
Remember it will search everything and might throw some errors (especially for NFS mounted file systems), but actual result will be shown at the bottom.

I hope this helps.
You think the OP meant directory listing - not pathname.
# 6  
Old 12-10-2013
Good point, MadeInGermany. What about
Code:
fullname="/f"
echo "${fullname##*/}"
f
echo "${fullname%${fullname##*/}}"
/

# 7  
Old 12-10-2013
We can allow for a file in the root filesystem or without a path thus:-
Code:
#!/bin/ksh

for fullname in "/a/b/c/d/e/f" "/f" "f"
do
   dir_name="${fullname%/*}"                                # Cut out the important bit
   dir_name="${dir_name:-/}"                                # If null, set to root
   [ "$dir_name" = "$fullname" ] && dir_name="."            # If same, then there is no path, so set current directory

   filename="${fullname##*/}"

   echo "You are working from ${fullname}, so a directory ${dir_name} and file ${filename}"
done

This gives me the output:-
Code:
You are working from /a/b/c/d/e/f, so a directory /a/b/c/d/e and file f
You are working from /f, so a directory / and file f
You are working from f, so a directory . and file f

Does that get us closer without having to start new shells to call dirname?



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to find and get a file in an entire directory with an excluded directory specified?

How to get a file 'zlib.h' in an entire directory with an excluded directory specified lives under that starting directory by using find command, as it failed on: $ find . -name 'zlib.h' -a -ipath 'CHROME.TMP' -prune -o -print it'll just list entirely up (2 Replies)
Discussion started by: abdulbadii
2 Replies

2. Shell Programming and Scripting

Shell script cannot create directory and move the file to that directory

I have a script, which is checking if file exists and move it to another directory if then mkdir -p ${LOCL_FILES_DIR}/cool_${Today}/monthly mv report_manual_alloc_rpt_A_I_ASSIGNMENT.${Today}*.csv ${LOCL_FILES_DIR}/cool_${Today}/monthly ... (9 Replies)
Discussion started by: digioleg54
9 Replies

3. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

4. Shell Programming and Scripting

Change to directory and search some file in that directory in single command

I am trying to do the following task : export ENV=aaa export ENV_PATH=$(cd /apps | ls | grep $ENV) However, it's not working. What's the way to change to directory and search some file in that directory in single command Please help. (2 Replies)
Discussion started by: saurau
2 Replies

5. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

6. Shell Programming and Scripting

Script which removes files from the first directory if there is a file in the second directory

Script must removes files from the first directory if there is a file with same name in the second directory Script passed to the two directories, it lies with them in one directory: sh script_name dir1 dir2 This is my version, but it does not work :wall: set - $2/* for i do set -... (6 Replies)
Discussion started by: SLAMUL
6 Replies

7. Shell Programming and Scripting

Move the latest or older File from one directory to another Directory

I Need help for one requirement, I want to move the latest/Older file in the folder to another file. File have the datetimestamp in postfix. Example: Source Directory : \a destination Directory : \a\b File1 : xy_MMDDYYYYHHMM.txt (xy_032120101456.txt) File2: xy_MMDDYYYYHHMM.txt... (1 Reply)
Discussion started by: pp_ayyanar
1 Replies

8. Shell Programming and Scripting

File transfer from one directory to another directory in unix

Hi, I have to transfer five files from one directory to another directory in unix with the help of shell scripts. This shell script calling the param file as input parameter. Every day one file will come and fall on my source directory. Remaining files will fall on any one of the day of the... (5 Replies)
Discussion started by: easterraj
5 Replies

9. Shell Programming and Scripting

Move a file from windows directory to unix directory

Move a file from windows directory to unix directory, is this possible? if it is, can someone help me on this? Thanks! God bless! (1 Reply)
Discussion started by: kingpeejay
1 Replies

10. UNIX for Dummies Questions & Answers

Copying one file at a time from one directory to another directory.

Hi All i want to write a script which could copy one file at a time from one directory to another directory. Scenerio: Let's say i have 100 file in a dirctory,so i want to copy one file at a time to another directory with a sleep statement in between that of 30 secs. please help me... (1 Reply)
Discussion started by: Nikhilindurkar
1 Replies
Login or Register to Ask a Question