LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-01-2013, 04:13 AM   #1
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Rep: Reputation: Disabled
Copy the List of all folder in a file


I want to list all the folders name of a directory in one file. I used the below set of commands to do that:

location="\usr\Richa\study"
cd $location
ls > ModelNames.txt


This one is working fine but in the below case
when i m taking the input from user , no entry added in the ModelNames file

echo "Please enter the location of models"
read location
cd $location
ls > ModelNames.txt



Note : In the above code \usr\Richa\study is the current directory, whereas location is another directory

Last edited by richa07; 07-01-2013 at 04:16 AM.
 
Old 07-01-2013, 04:38 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,317

Rep: Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331Reputation: 1331
Not sure I believe the first case worked since you are using backslashes instead of slashes.
 
Old 07-01-2013, 04:43 AM   #3
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Original Poster
Rep: Reputation: Disabled
Its a slashes only, i wrote it here manually

In the script it is slash only here by mistake I wrote backslash.

Can you please tell me how can i write all the folders(directory and files all) name in one file.
 
Old 07-01-2013, 05:00 AM   #4
Gad
Member
 
Registered: May 2013
Distribution: FreeBSD
Posts: 566

Rep: Reputation: 114Reputation: 114
Hi,

I may be no expert but i would presume the way would be to append the output to a file. Perhaps you can append the output >> or the "tee" command which would send the command to stdout and to a file.

Just my

Regards
Marios
 
Old 07-01-2013, 05:08 AM   #5
Madhu Desai
Member
 
Registered: Mar 2013
Distribution: Rocky, Fedora, Ubuntu
Posts: 541

Rep: Reputation: 153Reputation: 153
Try this, prints all directories:
PHP Code:
#!/bin/bash
read -"Please enter the location of models: "
find "$REPLY-maxdepth 1 -type d -fprint ModelNames.txt
# To display all sub-directories recursively
# find "$REPLY" -type d -fprint ModelNames.txt
cat ModelNames.txt 
Info:
-type d //only directories
-type f //only files

for both file and directories exclude -type option.

Last edited by Madhu Desai; 07-01-2013 at 05:11 AM.
 
2 members found this post helpful.
Old 07-01-2013, 05:16 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
Quote:
Note : In the above code \usr\Richa\study is the current directory, whereas location is another directory
Assuming the above is correct and that your follow up about the slashes being a typo is also true, then the below statement is dubious:
Quote:
This one is working fine
I would believe this more if your first script used a different location other than the current directory as you have no proof that your cd command actually worked.
 
1 members found this post helpful.
Old 07-01-2013, 06:02 AM   #7
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thumbs up Its working

Thanks for the post its working now

Can you tell me why ls was not working , I tried
ls |tee ModelName.txt
for this, script was printing the result in Standard output but was not able to copy it in the file.



Quote:
Originally Posted by mddesai View Post
Try this, prints all directories:
PHP Code:
#!/bin/bash
read -"Please enter the location of models: "
find "$REPLY-maxdepth 1 -type d -fprint ModelNames.txt
# To display all sub-directories recursively
# find "$REPLY" -type d -fprint ModelNames.txt
cat ModelNames.txt 
Info:
-type d //only directories
-type f //only files

for both file and directories exclude -type option.
 
Old 07-01-2013, 06:08 AM   #8
richa07
LQ Newbie
 
Registered: Jun 2013
Location: India
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thanks for the post

Hi
Thanks for the post
Please find the reply of the post below

When we copy the data to any file we can use > and >> both.
we use > when we want to override the data of file with new data
and >> to append the new data in the file

apart from that I used tee command , and script showing the ls result in standard output but text file is still empty.
I have no idea why its not updating the text file when its able to display the result.


Quote:
Originally Posted by mariose View Post
Hi,

I may be no expert but i would presume the way would be to append the output to a file. Perhaps you can append the output >> or the "tee" command which would send the command to stdout and to a file.

Just my

Regards
Marios
 
Old 07-03-2013, 12:03 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
If you're using a reasonably new version of bash, then you can generally use simple globbing, with the new recursive globstar pattern (**). Use printf to format the output one-file-per-line.

Code:
#!/bin/bash

shopt -s globstar dotglob nullglob 

location="/usr/Richa/study"
outfile="/path/to/ModelNames.txt"

printf '%s\n' "$location"/**/ >"$outfile"
If you add a backslash to the end of a glob pattern, it matches directories only.

Use dotglob to include hidden files.
Use b instead of nullglob if you want the command to error out if no match instead.

globstar is supported from bash v.4+, and there's currently one problem with it. If the file tree contains any recursive directory symlinks, it will get caught in an infinite loop and bog everything down. Other than that it should be very efficient.

ksh also supports this pattern out of the box (correction, you need to use set -G to enable it), and zsh probably does too, or something similar. I don't think they suffer from the above bug. Check their documentation though for full info on their available options.


An added benefit of globbing patterns is that you can also process them directly with a for loop, if you intend to do more with the files than just save their names.



PS: Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.

Last edited by David the H.; 07-03-2013 at 12:11 PM. Reason: correction + some reformatting
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to just copy the folder not the file ytyyutianyun Linux - Newbie 2 11-19-2011 04:47 AM
Scripts to list folder contents and copy images from folder and subfolders brunces Linux - Newbie 6 11-03-2011 01:23 PM
i cannot copy a file to a folder in file system with my root account realbezo Linux - Newbie 4 03-03-2009 10:45 PM
Copy a file into another directory and keep its folder hierarchy? Zebe Linux - General 2 11-25-2008 04:47 PM
copy a file to all subdirectories in a folder raj000 Linux - General 6 03-24-2006 03:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration