LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
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 03-27-2018, 11:24 AM   #1
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Rep: Reputation: Disabled
Question List folders with numbers in from using bash script


Hello

Looking for a way using a bash script to list all folders from /folderx/ and use in front numbers like:

1)foldera
2)folderb
3)folderc


Starting from the new one (by date) as number 1

Probably using ls (find command will list also hidden folders and i do not need them) and then pipe the results and do the magic there with the numbers?

If find is better to use in my case then ok no problem....

Thank you

Last edited by bmxakias; 03-27-2018 at 11:49 AM.
 
Old 03-27-2018, 11:35 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
You forgot to show what have you tried so far (it is your homework, isn't it?). Nonetheless, this might be useful:
Code:
ls -1tr | cat -n
 
Old 03-27-2018, 11:55 AM   #3
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Your command is almost what i want but i need to exclude the files and display only folders.....

How can i adjust your command to do that?

Thank you


My target is to display from folder /folderx/ all folders inside with numbers in front (by date) so the newest will have number 1 and let the user input a number and then read this number and run a command.....

I know how to do all the above except the listing of the folders

Last edited by bmxakias; 03-27-2018 at 11:56 AM.
 
Old 03-27-2018, 12:03 PM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
You forgot to show what have you tried so far (it is your homework, isn't it?).
 
Old 03-27-2018, 12:12 PM   #5
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Yes it is but as the directory listing is the first on the script i do not have anything yet

I need that to move down to the code....

As a sample:

Code:
#!/bin/bash

echo "`ls -1tr /folderx/ | cat -n`"
echo "Enter a number : \c"
read answer

case "$answer" in
     1)
        echo "SUCCESS";;

     2)
        echo "FAIL";;
esac
Thanks for your reply !

Last edited by bmxakias; 03-27-2018 at 12:47 PM.
 
Old 03-27-2018, 12:47 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
Impressive. Try this:
Code:
ls -1t | while read -r F; do test -d "$F" && echo "$F"; done | cat -n
Edit: changed 'ls -1tr' to 'ls -1t'
 
Old 03-27-2018, 12:59 PM   #7
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Yes this one works:

Code:
ls -1t | while read -r F; do test -d "$F" && echo "$F"; done | cat -n
My only issue now is that i can't get it working on a custom path like /folderx/ ..... it works well on the current path only...

I did try it as:

Code:
ls -1t /folderx/ | while read -r F; do test -d "$F" && echo "$F"; done | cat -n
Thank you

Last edited by bmxakias; 03-27-2018 at 01:02 PM.
 
Old 03-27-2018, 01:16 PM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
Guess you wanted ./folderx not /folderx/:
Code:
ls -1t ./folderx/ | while read -r F; do test -d "$F" && echo "$F"; done | cat -n
Gues
 
Old 03-27-2018, 01:22 PM   #9
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Doesn't work

Code:
ls: cannot access ./folderx: No such file or directory
It seems that is using the dot as part of the path

using cd /folderx it works so something else is wrong....

Last edited by bmxakias; 03-27-2018 at 01:25 PM.
 
Old 03-27-2018, 01:31 PM   #10
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
If cd /folderx works, then ls /folderx/ also works.

Edit: but if your /folderx directory doesn't contain subdirectories, you won't get any output.

Last edited by NevemTeve; 03-27-2018 at 01:40 PM.
 
Old 03-27-2018, 01:44 PM   #11
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
The ls command is ok as i can use this with no issues:

This one works:

Code:
echo "`ls -1t /folderx/`"
but this one doesn't work:

Code:
echo "`ls -1t /folderx/ | while read -r F; do test -d "$F" && echo "$F"; done | cat -n`"

I am getting a blank output and it seems the issue is at (only if i specify a folder as if i don't it work also):

Code:
| while read -r F; do test -d "$F" && echo "$F"; done | cat -n

Last edited by bmxakias; 03-27-2018 at 01:46 PM.
 
Old 03-27-2018, 02:45 PM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
Oh, I see 'ls' doesn't print the path.
Code:
Where=/folderx; ls -1t "$Where/" | while read -r F; do test -d "$Where/$F" && echo "$Where/$F"; done | cat -n

Last edited by NevemTeve; 03-27-2018 at 02:53 PM.
 
1 members found this post helpful.
Old 03-27-2018, 02:58 PM   #13
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Almost done

Now it works but the output has full path like:

Code:
1 /folderx/folder1

and my target is:

Code:
1 folder1
Thank you
 
Old 03-27-2018, 03:12 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,538

Rep: Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086Reputation: 1086
Guess it is high time to show some effort on your own.
 
Old 03-27-2018, 03:15 PM   #15
bmxakias
Member
 
Registered: Jan 2016
Posts: 121

Original Poster
Rep: Reputation: Disabled
Ok i fix it

Code:
Where=/folderx; ls -1t "$Where/" | while read -r F; do test -d "$Where/$F" && echo "$F"; done | cat -n
Thank you

Last edited by bmxakias; 03-27-2018 at 03:21 PM.
 
  


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
Bash Script to add numbers which has 0 prefix kingston Linux - Newbie 15 07-18-2015 01:23 PM
[SOLVED] Bash script: comparison with sequence of numbers? dbrazeau Programming 11 03-22-2011 12:22 PM
bash script to create folders including making recursive folders.... linux-bandit Linux - Software 8 11-28-2009 01:50 AM
Bash command to list installed libraries and version numbers newtovanilla Linux - Newbie 4 07-18-2008 04:49 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 09:01 PM

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

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