LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-08-2013, 12:52 AM   #1
Mperonen
LQ Newbie
 
Registered: Aug 2013
Posts: 2

Rep: Reputation: Disabled
Bash script issue (for loop inside a loop)


I have done the following script (below) to unlock all directories, which it does.

PROBLEM: How could I implement a second for loop inside the first one to loop through the current directory ($file) and to unlock all the content inside that specific directory?

Code:
for file in *
do
  sudo chmod ugo+rwx $file
done
 
Old 08-08-2013, 01:15 AM   #2
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Are you certain you want to do that?

making things rwxrwxrwx is seldom a good idea


For the purpose of illustration

Code:
for file in *
do
  if [ -d $file ]; then
    pushd $file
      sudo chmod ugo+rwx *
    popd
  else
    sudo chmod ugo+rwx $file
  fi
done
BUT that will only chmod files in current dir and 'one level below' dir

if you wanted to chmod ALL files/dir below current ( excluding hidden files/dirs, i.e. those being with "." , that are in the current), then

Code:
chmod ugo+rwx * -R
seriously, it is a bad idea.

why do you want/need to do this?

Last edited by Firerat; 08-08-2013 at 01:17 AM.
 
Old 08-08-2013, 01:30 AM   #3
Mperonen
LQ Newbie
 
Registered: Aug 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
Sometimes when using certain tools like file recovery etc the recovered files are automatically locked.
So I use this script only to unlock the files I really need to get rid of.

So yeah I'm sure of it (not unlocking anything important).

Thank you for your answer!
 
Old 08-08-2013, 02:14 AM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
well in that case,

chmod with -R
works the same for chown

but still I would use 755 ( rwxr-xr-x )

Code:
sudo chown <yourusername>:<yourusername> /path/to/recovered -R
sudo chmod 755 /path/to/recovered -R
and really 755 is too much, should be 644 for files and 755 for directories

Code:
sudo find /path/to/recovered -type f -exec chmod 644 {} ';'
sudo find /path/to/recovered -type d -exec chmod 755 {} ';'
sudo chown <yourusername>:<yourusername> /path/to/recovered -R
and if you are writing scripts to do it, you might as well do it safe
 
  


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Sleep inside for loop inside while loop causing issues. TheOnlyQ Programming 13 12-19-2012 12:59 PM
nested loop-bash script- issue on logic yathin Linux - Newbie 6 05-31-2010 06:30 AM

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

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