LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2023, 02:06 PM   #1
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441
Blog Entries: 43

Rep: Reputation: 36
Standard Scripts for Linux: Bash Script to List all Files Inside a Directory, Line by Line to Standard Output


I'm now in the process of updating and creating a working set of standard scripts for linux. This is the base, I will build everything else off of. Everything above it, will assume that the standard scripts are installed. They will eventually, want to be cross platform, at least on Windows and Linux. Since I'm focusing now on Linux, as it works better, this will be the first to be finished again. I'm writing here because I'm having some trouble. This is a network specific thing, for my home network. I may use them on other networks as time goes on, but let's start with my network. And then, soon after, I can work on my github page for my network, I hope, so that others can benefit possibly and stuff later, though I may start with making them private on there. We'll see. If I have to make them private, it's just because they contain some specific code identifying my network, which I may or may not want to share.

OK. Let's start with specifics, where I'm having trouble.

The first thing I'm going to want to do, is modify my install script, so that upon installing, as it's last task, it will output a log to its directory it's ran from, which will list in this format:

Code:
filename
filename
filename
...
..the files it installed, which can be gotten from listing in that format, the files in it's below directory of ./scripts/ all the files in there. Then, in my install file, I'll use that script in that folder, to output the files, line by line, and then I'll redirect it to a text (log) file. I want to include itself in this listing.

This, will then be able to be printed, by sending the text file to a printer, probably for now, at the library, and I will be able to have a checklist once it's on paper, of what's debugged, and what still needs to be debugged, very easily. Then I can come back to you guys and have some help debugging these files. Otherwise, I need a checklist by hand, which is trickier. I am losing time trying to create it by hand.
 
Old 08-23-2023, 02:07 PM   #2
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I know how to list, with the ls command, in general in bash. But this is a trickier problem, because I want it in a certain format. I assume it will just be a list command of some sort.
 
Old 08-23-2023, 02:16 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Code:
find /directory -type f
 
Old 08-23-2023, 03:11 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,781

Rep: Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935Reputation: 5935
Quote:
But this is a trickier problem, because I want it in a certain format.
It would help if you were more specific.
 
1 members found this post helpful.
Old 08-23-2023, 10:39 PM   #5
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I just mean I don't want it to list side by side, but as straight up and down. Let me try the command given, and see if it does what I want. Just a second...
 
Old 08-23-2023, 10:43 PM   #6
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
Almost! Just get rid of the directory name in the front of each thing, because I know that! I don't need the path in my output. Other than that, that commmand works just fine. As long as it really listed everything in the dir. I can't tell for sure, but that's because it's too hard to check, hence my reason for wanting the command in the first place.
 
Old 08-23-2023, 10:45 PM   #7
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
This is why, like I said in the other thread, I want to get commands listed by my everyday 2 workspaces!
 
Old 08-23-2023, 10:48 PM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,346
Blog Entries: 3

Rep: Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756Reputation: 3756
Try the -1 option for ls or the -printf option for find. Review them in "man ls" or "man find" to ensure that they do what you need.
 
Old 08-23-2023, 10:55 PM   #9
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,742

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Code:
ls -1 .  #the number one
Will list files in the current directory one file per line but the find command above will recurse down the directory tree.
Review man find about controlling the print output.
 
Old 08-23-2023, 11:58 PM   #10
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
I should have known that, but I didn't. The

Code:
ls -1 # Number
command, was just what I needed. Now I can find out, what I really need to debug! The tough work is ahead for these scripts! But at least it's going to be simpler with this list!
 
Old 08-24-2023, 12:00 AM   #11
des_a
Senior Member
 
Registered: Sep 2006
Posts: 1,441

Original Poster
Blog Entries: 43

Rep: Reputation: 36
My other scripts, with what's already is written, really IS tougher. But I had to have this list, so I could easily identify what I've already done.
 
Old 08-27-2023, 10:22 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,828

Rep: Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216Reputation: 1216
Code:
ls
only formats to columns if its output goes to a terminal.
Prove: redirect the output to a file
Code:
ls > list.txt
cat list.txt
Code:
printf "%s\n" *
is an alternative. The shell expands the *
and the printf command prints them.
(The format is re-applied until all arguments are printed. Compare with
Code:
printf "%s\t\t%s\n" *
)

Last edited by MadeInGermany; 08-27-2023 at 10:23 AM.
 
1 members found this post helpful.
Old 08-31-2023, 12:28 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
NB :
Code:
ls -1
lists files & dirs in a dir (but not hidden ones), in case that matters

Last edited by chrism01; 08-31-2023 at 12:30 AM.
 
Old 01-12-2024, 06:25 PM   #14
maybeJosiah
Member
 
Registered: Jan 2024
Location: USA
Distribution: Ubuntu, Android, Ubuntu Touch, Jolla Sailfish maybe
Posts: 235
Blog Entries: 2

Rep: Reputation: 5
ls -a

Not sure if it helps but I know "ls -a" lists even hidden stuff. Maybe if you make it an array and pipe it through a function that does line by line. grep may be useful. X E.
 
  


Reply



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
[BASH] Multiplex the output of two or more commands to standard output ychaouche Programming 12 09-30-2020 10:07 AM
[SOLVED] Bash Script to Create Menu Based on a File List (Map Files List to Numbers) Steve R. Programming 2 07-08-2020 05:03 PM
[SOLVED] I am trying to write a very simple recursive bash script to chmod 644 all files and chmod 755 all directories in a given directory Astral Axiom Programming 23 05-31-2019 07:44 AM
Standard script output to screen & -x option output to file BoBeau236 Programming 6 05-10-2011 07:43 AM
[SOLVED] Partial list with ls-l in bash script run in cron but full list run from command line redgshost Linux - General 29 01-16-2011 12:14 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:43 AM.

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