PDA

View Full Version : Shell: how can I print into a file content of all subfolders of a folder?



vincegata
December 30th, 2012, 07:08 PM
Hello,

Right now my script looks like this (I have more subfolders actually):



echo "************************ Finance Papers ************************" > /home/ListOfPapers.txt # Add header
txt -o /home/vince/Papers/Papers_Finance >> /home/ListOfPapers.txt # Add list of papers in the folder.
echo >> /home/vince/Documents/ListOfPapers.txt # Add empty line.

echo "************************ Math Papers ************************" >> /home/ListOfPapers.txt
txt -o /home/vince/Papers/Papers_Math >> /home/ListOfPapers.txt
echo >> /home/vince/Documents/ListOfPapers.txt

echo "************************ Programming Papers ************************" >> /home/ListOfPapers.txt
txt -o /home/vince/Papers/Papers_Prog >> /home/ListOfPapers.txt
echo >> /home/ListOfPapers.txt


With such script, when I add or delete subfolders in /home/vince/Papers/ folder I need to edit my script.

How can I write a script so it will navigate each subfolder and print its contents (in the same format: header, list of files, space.)

Thanks in advance.

ehrt74
December 30th, 2012, 07:54 PM
Have you tried playing around with something like:



for i in `find . -type d`
do
echo $i
ls $i
done

vincegata
December 30th, 2012, 08:20 PM
Have you tried playing around with something like:



for i in `find . -type d`
do
echo $i
ls $i
done


It's good -- thank you!

The only thing is it cannot handle folders with spaces, such as
/Papers Numerical Analysis

Is it possible to fix it, o.w. I'll just rename the folders.

THX

HomelandSecurity
December 30th, 2012, 08:23 PM
for i in `find . -type d`
do
echo "$i" ls "$i"
done


what about the subfolders of a folder into the selcte folder? the you'll have to use find not ls!

Cheesemill
December 30th, 2012, 08:36 PM
How about just using the tree command (you may need to install it first).


rob@arch:~$ tree ~/ISOs
/home/rob/ISOs
├── haiku-r1alpha4.iso
├── Linux
│ ├── archlinux-2012.11.01-dual.iso
│ ├── archlinux-2012.12.01-dual.iso
│ ├── BT5R3-GNOME-64.iso
│ ├── CrunchBang
│ │ ├── crunchbang-11-20120430-amd64.iso
│ │ ├── crunchbang-11-20120430-i386.iso
│ │ └── crunchbang-11-20121015-amd64.iso
│ ├── debian-6.0.6-amd64-businesscard.iso
│ ├── debian-wheezy-DI-b3-amd64-netinst.iso
│ ├── linuxmint-14.1-cinnamon-dvd-64bit.iso
│ └── Ubuntu
│ ├── Precise
│ │ ├── edubuntu-12.04.1-dvd-amd64.iso
│ │ ├── kubuntu-12.04.1-alternate-amd64.iso
│ │ ├── kubuntu-12.04.1-alternate-i386.iso
│ │ ├── kubuntu-12.04.1-desktop-amd64.iso
│ │ ├── kubuntu-12.04.1-desktop-i386.iso
│ │ ├── lubuntu-12.04-alternate-amd64.iso
│ │ ├── lubuntu-12.04-alternate-i386.iso
│ │ ├── lubuntu-12.04-desktop-amd64.iso
│ │ ├── lubuntu-12.04-desktop-i386.iso
│ │ ├── ubuntu-12.04.1-alternate-amd64.iso
│ │ ├── ubuntu-12.04.1-alternate-i386.iso
│ │ ├── ubuntu-12.04.1-desktop-amd64.iso
│ │ ├── ubuntu-12.04.1-desktop-i386.iso
│ │ ├── ubuntu-12.04.1-server-amd64.iso
│ │ ├── ubuntu-12.04.1-server-i386.iso
│ │ ├── ubuntu-12.04-business-desktop-remix-amd64.iso
│ │ ├── ubuntu-12.04-business-desktop-remix-i386.iso
│ │ ├── ubuntu-12.04-cloud-live-amd64.iso
│ │ ├── ubuntu-12.04-mini-amd64.iso
│ │ ├── ubuntu-12.04-mini-i386.iso
│ │ ├── ubuntu-12.04-mini-i386-nonpae.iso
│ │ ├── ubuntustudio-12.04.1-dvd-amd64.iso
│ │ ├── xubuntu-12.04.1-alternate-amd64.iso
│ │ ├── xubuntu-12.04.1-alternate-i386.iso
│ │ ├── xubuntu-12.04.1-desktop-amd64.iso
│ │ └── xubuntu-12.04.1-desktop-i386.iso
│ ├── Quantal
│ │ ├── edubuntu-12.10-dvd-amd64.iso
│ │ ├── kubuntu-12.10-desktop-amd64.iso
│ │ ├── kubuntu-12.10-desktop-i386.iso
│ │ ├── lubuntu-12.10-alternate-amd64.iso
│ │ ├── lubuntu-12.10-alternate-i386.iso
│ │ ├── lubuntu-12.10-desktop-amd64.iso
│ │ ├── lubuntu-12.10-desktop-i386.iso
│ │ ├── mini.iso
│ │ ├── ubuntu-12.10-desktop-amd64.iso
│ │ ├── ubuntu-12.10-desktop-i386.iso
│ │ ├── ubuntu-12.10-server-amd64.iso
│ │ ├── ubuntu-12.10-server-i386.iso
│ │ ├── ubuntu-secure-remix-12.10-64bit.iso
│ │ ├── ubuntustudio-12.10-dvd-amd64.iso
│ │ ├── ubuntustudio-12.10-dvd-amd64.iso.zs-old
│ │ ├── xubuntu-12.10-desktop-amd64.iso
│ │ └── xubuntu-12.10-desktop-i386.iso
│ ├── Raring
│ │ ├── kraring-alternate-amd64.iso
│ │ ├── kraring-alternate-i386.iso
│ │ ├── kraring-desktop-amd64.iso
│ │ ├── kraring-desktop-i386.iso
│ │ ├── lraring-alternate-amd64.iso
│ │ ├── lraring-alternate-i386.iso
│ │ ├── lraring-desktop-amd64.iso
│ │ ├── lraring-desktop-i386.iso
│ │ ├── mini.iso
│ │ ├── raring-desktop-amd64.iso
│ │ ├── raring-desktop-i386.iso
│ │ ├── raring-server-amd64.iso
│ │ ├── raring-server-i386.iso
│ │ ├── xraring-desktop-amd64.iso
│ │ └── xraring-desktop-i386.iso
│ ├── ubuntu-10.04.4-server-amd64.iso
│ ├── ubuntu-8.04-desktop-i386.iso
│ └── warty-release-install-i386.iso
├── Microsoft
│ └── WIN7_SP1 AIO_EN_DVD.ISO
├── oi-dev-151a5-text-x86.iso
└── sol-11_1-text-x86.iso


If you look at the man page there are loads of different options you can use, for example the -o option will output to a file instead of the screen.

ofnuts
December 31st, 2012, 01:04 AM
Have you tried playing around with something like:
for i in `find . -type d` do echo $i ls $i done You can use the shorter (and less prone to parsing problems): To print file path/names:
find . To print file information:
find . -ls To execute a command against each file:
# {} is replaced by the name of each file # (no need to use quotes even if spaces are expected) find . -exec somecommand {} \;

vincegata
December 31st, 2012, 01:13 AM
Thank you for all responses!

vincegata
December 31st, 2012, 01:17 AM
You can use the shorter (and less prone to parsing problems): To print file path/names:
find . To print file information:
find . -ls To execute a command against each file:
# {} is replaced by the name of each file # (no need to use quotes even if spaces are expected) find . -exec somecommand {} \;

It does take care of spaces in folders, how do I insert name of folder and space?

Header (name of a folder)
List of files
Space