Printing directory starting from a particular folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Printing directory starting from a particular folder
# 1  
Old 04-23-2012
Printing directory starting from a particular folder

Hi there!Smilie
I have a question if I have a directories
Code:
~/MyFolder/Files/PersonalFiles/databases/food/tomato
~/MyFolder/Files/PersonalFiles/databases/items/cup

and I want my program to output food/tomato and items/cup, i.e. everything from the folder database but without "~/MyFolder/Files/personalFiles/databases/" . Is there are a way to do this i tried to do
Code:
echo sed 's/^.*databases//' -d

but the output is still with ~/MyFolder/ Files etc. Would much appreciate any help!Smilie


Moderator's Comments:
Mod Comment Welcome to the UNIX and Linux Forums. Please use [code] tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-24-2012 at 12:56 AM..
# 2  
Old 04-24-2012
One method using the unix basename and dirname commands.

Code:
filename1="~/MyFolder/Files/PersonalFiles/databases/food/tomato"
var1=$(basename $(dirname "${filename1}"))
var2=$(basename "${filename1}")
echo "$var1/$var2"

filename2="~/MyFolder/Files/PersonalFiles/databases/items/cup"
var3=$(basename $(dirname "${filename2}"))
var4=$(basename "${filename2}")
echo "$var3/$var4"

./scriptname
food/tomato
items/cup

Depending on what Shell you have, there are other pure Shell methods of achieving the desired effect.
This User Gave Thanks to methyl For This Post:
# 3  
Old 04-24-2012
Your sed was almost there:

Code:
echo "~/MyFolder/Files/PersonalFiles/databases/food/tomato"|sed 's/^.*databases\///g'

food/tomato


echo "~/MyFolder/Files/PersonalFiles/databases/items/cup"|sed 's/^.*databases\///g'

items/cup

This User Gave Thanks to methyl For This Post:
# 4  
Old 04-24-2012
Don't forget the obvious:

Code:
( cd ~/MyFolder/Files/PersonalFiles/databases/
        ls food/tomato
        ls items/cup )

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 04-24-2012
thanks a lot but if I don't know the directory I just know that it is in home directory and it should be in datasheet folder somewhere in home directory how can I do that then???
# 6  
Old 04-24-2012
~/datasheet ?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Homework & Coursework Questions

Bash script for printing folder names and their sizes

1. The problem statement, all variables and given/known data: The task is to create a script that would reproduce the output of 'du' command, but in a different way: what 'du' does is: <size> <folder name>and what is needed is <folder name> <size>We need to show only 10 folders which are the... (3 Replies)
Discussion started by: UncleIS
3 Replies

3. Shell Programming and Scripting

Bash script for printing folder names and their sizes

Good day, everyone! I'm very new to bash scripting. Our teacher gave us a task to create a script that basically does the same job the 'du' command does, with the difference that 'du' command gives an output in the form of <size> <folder name>and what we need is <folder name> <size>As for... (1 Reply)
Discussion started by: UncleIS
1 Replies

4. What is on Your Mind?

Directory vs. Folder?

How would you call it? (24 Replies)
Discussion started by: absorber
24 Replies

5. Shell Programming and Scripting

Find a folder from a directory

Hi, I have a folder name XX and a directory N:. Now I want to find the folder on that directory and print the path. I tried with grep "XX" N: but also search for files named as XX, but I want to find onlt folder and print. please help me.. Thanks in advance (12 Replies)
Discussion started by: arup1980
12 Replies

6. Shell Programming and Scripting

Checking files in folder using starting string for filename

Hi, How do i check if there are any files present in the folder with some specific starting string. For eg :- I have used this where Source_File is filename parameter. if then return 2 fi But in my case the source file name is not constant. The only constant thing is... (10 Replies)
Discussion started by: chetancrsp18
10 Replies

7. Shell Programming and Scripting

How to count the number of files starting with a pattern in a Directory

Hi! In our current directory there are around 35000 files. Out of these a few thousands(around 20000) start with, "testfiles9842323879838". I want to count the number of files that have filenames starting with the above pattern. Please help me with the command i could use. Thank... (7 Replies)
Discussion started by: atechcorp
7 Replies

8. Shell Programming and Scripting

printing a directory structure

Hey everyone I am just trying print a directory structure but I am not getting the desired output. I am using AIX. Script : input file contains : TNP\\ECOM\\test\\1 input_file=folders.txt cat $input_file | while read line do echo $line done (3 Replies)
Discussion started by: rocker_me2002
3 Replies

9. UNIX for Dummies Questions & Answers

starting bash in a directory

I wish to start bash in a directory at a per invocation basis. i.e, this has nothing to do with a user's home directory. In my program, I should be able to give: "bash "/lib/examples" in one place and bash "/lib/src" in another. A shell would start in that folder for me to work in. This... (2 Replies)
Discussion started by: sentinel
2 Replies

10. Shell Programming and Scripting

Printing files from a directory

Hi, I need help in printing files from a directory. eg: a directory called /home/ABC and there are 3 files in it. I need to give printout one bye one. One more thing is I need to print date and the path on the top of each page...like... Sep 30 14:22 2008 /home/ABC Page 1 I've been... (1 Reply)
Discussion started by: injeti
1 Replies
Login or Register to Ask a Question