LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-19-2016, 02:49 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question Can variables be used in multiple directory {f1,f2,...,N} syntax?


Can variables be used with the multiple directory {folder1,folder2,...,folder N} syntax?

I would like to check how many total files are in four specific sub-directories of the ~/.config/cairo-dock directory. I successfully find out how many by the command:
Code:
$ find ~/.config/cairo-dock/{current_theme,stack,themes,third-party} -type f | wc -l
829
I would like to use a variable to replace the {folder1,folder2,...,folder N} syntax. E.g.
Code:
DIR=current_theme,stack,themes,third-party
and then
Code:
$ find ~/.config/cairo-dock/{${DIR}} -type f | wc -l
Unfortunately this results in
Code:
find: `/home/a/.config/cairo-dock/{current_theme,stack,themes,third-party}': No such file or directory
Whenever the ${DIR} is used and not the longhand {current_theme,stack,themes,third-party} it doesn't work.

Using variables would be more convenient in a script where the {current_theme,stack,themes,third-party} is repeated.

Some attempts are below:

ATTEMPTS
I first set the variable:
Code:
DIR=current_theme,stack,themes,third-party
Next , I check that this variable will display correct text:
Code:
$ echo "/home/a/.config/cairo-dock/{${DIR}}"
/home/a/.config/cairo-dock/{current_theme,stack,themes,third-party}
I then try to count the number of files by either find or ls:
Code:
$ find $(echo "/home/a/.config/cairo-dock/{$(echo "${DIR}")}") -type f
$ ls -d $(echo "/home/a/.config/cairo-dock/{$(echo "${DIR}")}")
$ find $(echo "/home/a/.config/cairo-dock/{$(echo "$DIR")}") -type f
$ ls -d $(echo "/home/a/.config/cairo-dock/{$(echo "$DIR")}")
$ ls -d /home/a/.config/cairo-dock/{$(echo "$DIR")}
$ ls -d /home/a/.config/cairo-dock/{$(echo `$DIR`)}
$ ls -d /home/a/.config/cairo-dock/{$DIR}
$ ls -d /home/a/.config/cairo-dock/{$(echo `$DIR}`)
$ ls -d /home/a/.config/cairo-dock/{$DIR}
$ ls -d $(echo "/home/a/.config/cairo-dock/{$DIR}")
All of the above attempts result in one of the following errors:
Code:
ls: cannot access /home/a/.config/cairo-dock/{current_theme,stack,themes,third-party}: No such file or directory
OR
Code:
find: `/home/a/.config/cairo-dock/{current_theme,stack,themes,third-party}': No such file or directory

Last edited by andrew.comly; 01-19-2016 at 02:54 AM. Reason: grammar
 
Old 01-19-2016, 03:27 AM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
well if its just to find teh no of files in sub directories, i would say the approach doesn't looks good, you are using find where you already know the location of files :

A simple code for your requirement though it would not be the compact of all which can be achieved.

Code:
for i in /home/a/.config/cairo-dock/; do echo -e "$i:\c ";ls $i|wc -l; done

Last edited by SAbhi; 01-19-2016 at 03:30 AM. Reason: re-phrase
 
1 members found this post helpful.
Old 01-20-2016, 04:45 AM   #3
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile

Quote:
Originally Posted by SAbhi View Post
A simple code for your requirement though it would not be the compact of all which can be achieved.
Code:
for i in /home/a/.config/cairo-dock/; do echo -e "$i:\c ";ls $i|wc -l; done
Yes, I guess I am too curious for learning a quicker way.

I can just first Define the variable FLDRS:
Code:
FLDRS="/home/a/.config/cairo-dock/current_theme /home/a/.config/cairo-dock/stack /home/a/.config/cairo-dock/themes /home/a/.config/cairo-dock/third-party"
and use a simple "for $i in `echo "$FLDRS"`; do ... done" loop like above.

Curiosity is good but excessive curiosity can cause one to waste time chasing after illusions. Often times the simplest way is the best.
 
Old 01-21-2016, 10:12 AM   #4
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
Quote:
Originally Posted by andrew.comly View Post
Yes, I guess I am too curious for learning a quicker way.

I can just first Define the variable FLDRS:
Code:
FLDRS="/home/a/.config/cairo-dock/current_theme /home/a/.config/cairo-dock/stack /home/a/.config/cairo-dock/themes /home/a/.config/cairo-dock/third-party"
and use a simple "for $i in `echo "$FLDRS"`; do ... done" loop like above.

Curiosity is good but excessive curiosity can cause one to waste time chasing after illusions. Often times the simplest way is the best.
well using `echo $FLDRS` is again a useless use inside for when only $FLDRS can give you what you need.
 
1 members found this post helpful.
Old 01-22-2016, 11:52 PM   #5
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Lightbulb Solution

Answered! (But on another thread)

To those of us who hold safety as a top priority, due to bash's order of expansion using variables inside brackets simply is not an efficient practice. Please peruse the answer below from http://www.linuxquestions.org/questi...rs-4175563266/.

Quote:
Originally Posted by rknichols View Post
The relevant paragraph from the bash manpage is under EXPANSION:
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion.
Brace expansion is performed before variable expansion, so "{${ALPHA}}" isn't seen as a legitimate brace expansion (there's no comma inside those braces) and is left as-is. (The output from "-x" puts quotes around it since it contains characters that are special to the shell.)

You can force the expression to be re-parsed after variable expansion by using eval, but that is extremely dangerous unless you have absolute confidence that the string being passed to eval can't contain anything dangerous like content from an outside source, since that could include things like command substitution running arbitrary commands. If you are sure that the content of ALPHA is always safe, then
Code:
eval mkdir -p 1 {${ALPHA}}

No Sir, I'm not forcing anything!! Safety prevents mishaps. Preventive maintenance saves time in the long run.
 
  


Reply

Tags
bash, directories, ls, multi, scripts



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Would like to "echo" a line, "read" reply, then execute the reply: How in bash? UnixAgain Linux - Newbie 6 02-03-2015 08:27 AM
A question about "echo "$CALLER" | tr -s '[:upper:]' '[:lower:]'" thomas2004ch Linux - Software 5 03-13-2012 02:57 PM
c gurus: whats the diff? "var[]" and "*var"? kourama Programming 16 04-03-2007 10:28 AM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:03 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