LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 05: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 05:16 AM.
 
Old 07-01-2013, 05:38 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,614

Rep: Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703Reputation: 703
Not sure I believe the first case worked since you are using backslashes instead of slashes.
 
Old 07-01-2013, 05: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, 06:00 AM   #4
mariose
Member
 
Registered: May 2013
Location: South Africa
Distribution: Debian
Posts: 177
Blog Entries: 1

Rep: Reputation: 7
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, 06:08 AM   #5
mddnix
Member
 
Registered: Mar 2013
Distribution: Redhat, Ubuntu
Posts: 516

Rep: Reputation: 139Reputation: 139
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 mddnix; 07-01-2013 at 06:11 AM.
 
2 members found this post helpful.
Old 07-01-2013, 06:16 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,256

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
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, 07: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, 07: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, 01:03 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 01: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 05:47 AM
Scripts to list folder contents and copy images from folder and subfolders brunces Linux - Newbie 6 11-03-2011 02:23 PM
i cannot copy a file to a folder in file system with my root account realbezo Linux - Newbie 4 03-03-2009 11:45 PM
Copy a file into another directory and keep its folder hierarchy? Zebe Linux - General 2 11-25-2008 05:47 PM
copy a file to all subdirectories in a folder raj000 Linux - General 6 03-24-2006 04:55 AM


All times are GMT -5. The time now is 10:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration