LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-27-2018, 03:21 PM   #16
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869

> For my knowledge i think i will cd to folderx and then run the command to show all folders...

Now that is a good idea. You might even start a subshell (using parentheses):
Code:
(cd /folderx; insertcommandshere)
The 'cd' in the subshell doesn't affect the main shell.

Last edited by NevemTeve; 03-27-2018 at 03:22 PM.
 
1 members found this post helpful.
Old 03-27-2018, 03:23 PM   #17
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Is it better to use it with the trailing slash?

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

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

Last edited by bmxakias; 03-27-2018 at 03:31 PM.
 
Old 03-27-2018, 03:35 PM   #18
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
I think these two do the same thing.
 
Old 03-27-2018, 03:37 PM   #19
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Ok thank you very much !!!!

I will continue with that script now
 
Old 03-28-2018, 10:32 AM   #20
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A variant with cd; do it in a (sub shell) if you want the current work dir unchanged.
A variable num obsoletes the cat -n
Code:
(num=0; cd /folderx && ls -t | while IFS= read -r F; do test -d "$F" && echo "$((num+=1)) $F"; done)
 
Old 03-28-2018, 10:43 AM   #21
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,789

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Quote:
Originally Posted by bmxakias View Post
Is it better to use it with the trailing slash?

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

Code:
Where=/folderx/; ls -1t "$Where" | while read -r F; do test -d "$Where$F" && echo "$F"; done | cat -n
The second requires /folderx to be a folder. Will give error if it happens to be an existing file.
 
Old 03-28-2018, 01:20 PM   #22
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
ok i am using the:

Code:
Where=/folderx/; ls -1t "$Where" | while read -r F; do test -d "$Where$F" && echo "$F"; done | cat -n
and i am getting an output like:

Code:
1 folder123
2 folder9584958
3 folderelse
So if i select for example 2 how should i get the name of the folder to create a variable for it?

I am using:

Code:
   read answer
   case "$answer" in
     1) 
    
     2)

     3)
Thank you

Last edited by bmxakias; 03-28-2018 at 01:35 PM.
 
Old 03-28-2018, 11:33 PM   #23
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Maybe you could store the filenames in a bash-array. Read 'man bash' for details. Also here's some basics:
Code:
declare -a Files
Files=(*c)
echo ${#Files} # number of elements
echo ${Files[0]} # first element
for i in "${Files[@]}"; do printf '%s\n' "$i"; done # all elements
 
Old 03-29-2018, 07:27 AM   #24
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Doesn't sound very easy for me

I will wait for an easier solution or a sample if anyone can help..

Thanks for your reply....
 
Old 03-29-2018, 07:40 AM   #25
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,860
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Maybe you could consult your teacher, explaining them that this homework assignment is not suitable for your knowledge-level.
 
Old 03-29-2018, 12:47 PM   #26
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
I am not a student

I am too old for that i think

I am just try to create a script that will automate some jobs for me and in fact all are ready and i only miss that part above....

If i don't find any solution i will bypass that part and i will set a variable for it that i have to fill before use it and problem solved

But i really like to learn a solution for this as it may help in future...

Thank you
 
Old 03-29-2018, 02:59 PM   #27
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Maybe select would be appropriate?
Code:
select dir in $(ls -1td /folderx/*/|sed 's/\/*$//');do 
    echo $dir; 
done
Combine this with case statements and it should be done

Some help pages
Code:
help select
help case
http://tldp.org/LDP/Bash-Beginners-G...ect_09_06.html

Last edited by keefaz; 03-29-2018 at 03:02 PM.
 
1 members found this post helpful.
Old 03-29-2018, 04:00 PM   #28
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Yes that seems to work also

The only issue is that the output is like:

Code:
1) /folderx/folder1
2) /folderx/folder2
3) /folderx/folder3
and my target is to not have the full path as output and get only the last folder name like:

Code:
1) folder1
2) folder2
3) folder3

Any help on that please?

Thank you
 
Old 03-29-2018, 04:46 PM   #29
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Yes, one way to do it:
Code:
select dir in $(ls -1td /folderx/*/| awk -F/ '{print $(NF-2)}'; do
...
 
Old 03-30-2018, 04:11 AM   #30
bmxakias
Member
 
Registered: Jan 2016
Posts: 254

Original Poster
Rep: Reputation: Disabled
Thanks for your reply.

A parenthesis was missing at the end that i add:

Code:
select dir in $(ls -1td /folderx/*/| awk -F/ '{print $(NF-2)}'); do
but the result using this command is to list the folderx itself repeated like:

Code:
1) folderx
2) folderx
3) folderx
and not the contents of the folderx by date

Last edited by bmxakias; 03-30-2018 at 04:32 AM.
 
  


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 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 01:57 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
Open Source Consulting | Domain Registration