Link to home
Start Free TrialLog in
Avatar of Jorge Lazo
Jorge Lazo

asked on

Linux Bash Script - List file directories on hard drive

Hello all,

I am attempting to write a Linux bash script to read the file directory off an external drive. i would like to be able to pull a quick list of the file directories so that i can save them for future reference.

thank you in advance
Avatar of Jorge Lazo
Jorge Lazo

ASKER

i know that if i do the below line it will properly pull the file directory into that file, however it is pulling it for my hard drive and not my external drive.
ls > filename.txt

Open in new window

Avatar of David Favor
I'm guessing you mean contents of file directory.

So something like this...

for file in $dir/* ; do echo $file ; done

Open in new window

Maybe mention what "future use" might mean.

In a file, like Jorge suggested or to use later in a script as I suggested.

Give an exact description of what you'll use these for.

Also... occurs to me... you might be doing this because you have many files in a directory. Mention file count, if the count is large.
THe following will recurse through all directories on a disk. And show all files including hidden files.
ls -laR  /mountpoint >/archive/directory/TheDiskName.list

another one might be:

find /mountpoint -ls >/archive/directory/TheDiskName.list
ASKER CERTIFIED SOLUTION
Avatar of kenfcamp
kenfcamp
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can also use PERL for this. The code will be far simpler + easier to read...

my $dir = "path-to-your-directory";
my @files = glob("$dir/*");

foreach my $file (@files) {
   # do something here with $file
}

Open in new window

i was able to use
 ls -LR /Volumes/Drive/> textfile.txt

Open in new window


Thank you everyone!