Getting file without mentioning the path


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Getting file without mentioning the path
# 1  
Old 04-09-2016
Getting file without mentioning the path

If I am in
Code:
C:\WINDOWS\Resources\Themes

and I have a file in an other directory called
Quote:
example.txt
to which I want to go but I do not know the path to it . Using a Linux command , is there a way to go directly to it without knowing its.
# 2  
Old 04-09-2016
You would use the find command ..

Code:
find some_top_level_directory -name example.txt

# 3  
Old 04-10-2016
What do you mean by "go there"? Edit the file? Run the file? Use it some other way? What way?
As there may be many files with that name, Scrutinizer's proposal will show them all but won't "go there". You can edit a file in any path immediately like
Code:
vi path/shown/by/Scrutinizers/proposal/example.txt

# 4  
Old 04-20-2016
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.

Last edited by Don Cragun; 04-20-2016 at 05:33 PM.. Reason: Change ICODE tags to CODE tags.
# 5  
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.
 
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 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question