Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Getting file without mentioning the path Post 302971481 by Don Cragun on Wednesday 20th of April 2016 05:05:21 PM
Old 04-20-2016
Quote:
Originally Posted by ashish_samarth
Try Below

Code:
find ./* -type f -iname "source_file.txt" -exec dirname {} \; 2> /dev/null | cd `awk '{print $0}'`

Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) for full-line and multi-line sample input, output, and code segments.
This might work on some systems using some shells, but there are a few problems:
  1. Using find ./* ... can give you argument list too long errors when invoked in directories containing lots of files and/or several files with long filenames. It would be better to use find . ....
  2. If there is a file named source_file.txt in more than one directory in the file hierarchy rooted in the current working directory, you will be invoking cd with two or more operands instead of the single operand you're assuming will be specified.
  3. The standards say that commands in a pipeline can be executed in a subshell environment. If cd is executed in a subshell environment, the current working directory of the shell execution environment you will be in when the pipeline completes will not have been changed.
If the intent is to move into the directory containing a file named example.txt and do something in that directory for each directory that contains a file by that name, something more like:
Code:
#!/bin/ksh
spot=$PWD
find . -name 'example.txt' | while read -r path
do	file=${path##*/}
	dir=${path%/*}
	if ! cd "$dir"
	then	continue
	fi
	printf 'Process file "%s" in directory "%s"\n' "$file" "$dir"
	# do whatever you want to do in this directory...
	cd "$spot"	# return to the directory in which we started
done

This was written and tested using a Korn shell, but this should work with any shell that performs the basic parameter expansions specified in the POSIX standards.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

vi - replacing a relative path with absolute path in a file

Hi, I have a file with about 60 lines of path: app-defaults/boxXYZ....... I want to change this to /my/path/goes/here/app-defaults/boxXYZ, but of course vi doesn't like the regualr :s/old/new/ command. Is there any other quick way to do this? Thanks ;) (2 Replies)
Discussion started by: Yinzer955i
2 Replies

2. Shell Programming and Scripting

serach and replace file name in the path in a remote xml file

hi every one , here is my problem !! i have to run my script from an account and update the result in a xml file located on a different account. i use existing ssh keys to do it remotely for example the tags looks like this <PropertyValueList... (1 Reply)
Discussion started by: kiranreddy1215
1 Replies

3. UNIX for Dummies Questions & Answers

find locked files, print file path, unlock file

using OS X and the Terminal, I'd like to find all locked files in a specified directory, unlock them, and print a list of those files that were unlocked how can I do this? I'm familiar with chflags nouchg for unlocking one file but not familiar with unix enough to do what I'd like. Thanks! (0 Replies)
Discussion started by: alternapop
0 Replies

4. Shell Programming and Scripting

Retrieve directory path from full file path through sh

Hi, I have a file abcd.txt which has contents in the form of full path file names i.e. $home> vi abcd.txt /a/b/c/r1.txt /q/w/e/r2.txt /z/x/c/r3.txt Now I want to retrieve only the directory path name for each row i.e /a/b/c/ /q/w/e/ How to get the same through shell script?... (7 Replies)
Discussion started by: royzlife
7 Replies

5. Shell Programming and Scripting

Recursive file processing from a path and printing output in a file

Hi All, The script below read the path and searches for the directories/subdirectories and for the files. If files are found in the sub directories then read the content of the all files and put the content in csv(comma delimted) format and the call the write to xml function to write the std... (1 Reply)
Discussion started by: Optimus81
1 Replies

6. UNIX for Advanced & Expert Users

Need help on moving .csv file from UNIX to windows file path

Need help on moving .csv file from unix to windows file path. (1 Reply)
Discussion started by: lakshmanraok117
1 Replies

7. Shell Programming and Scripting

Use sendmail without mentioning From:

Hi, Here is my sendmail script. #!/bin/ksh { # print "From: user1@`hostname`" print "To: me@mycomp.com" } | /usr/sbin/sendmail -t I wish to use sendmail without having to specify # print "From: user1@`hostname`" so that it picks or formulates the default value of... (1 Reply)
Discussion started by: mohtashims
1 Replies

8. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

9. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

10. UNIX for Beginners Questions & Answers

How do I add a log file path to a vi file to monitor all the changes made to the file?

I'm curious to know how do I add an empty log file (test1.log) to an existing text file to monitor all the changes made to a.txt. Is this expression export PATH=$PATH:/home/test1.log right to be added to the text file a.txt? (5 Replies)
Discussion started by: TestKing
5 Replies
All times are GMT -4. The time now is 09:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy