LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 05-18-2011, 07:33 AM   #1
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Rep: Reputation: 127Reputation: 127
Change "rwxr-xr-x" to "755"


What I want to do is write a small bash-script that reads a directory-structure on serverA so I then can create an identical structure on serverB, including ownership & permission.
I do not want to simply copy everything it is far too much so no files, only directories.

What I have now is:
Code:
find /home/pingu/test/Cert -type d -ls |awk '{ print $3 " " $5" " $6" " $11}'> mydir
sed -i  s/.// mydir
Prints out
Code:
rwxr-xr-x pingu users /home/pingu/test/Cert
rwxr-xr-x pingu users /home/pingu/test/Cert/Dokument
To then read this file "mydir" into an array and create the directories with correct owner&group is a piece of cake (I believe w/o having tried yet...)
Problem: To set permissions I need to use numbers. So how do I change that "rwx.."-style to numbers ("755")? Please note: the permissions vary a lot in the real directories, I can't just set "750" have to be very careful to get correct permissions.
cp, ls, tar, find... - none of them has built-in options that does what I want, have searched docs & manpages, tried a lot of combinations but to no avail.
Any suggestions?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 05-18-2011, 07:44 AM   #2
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

I don't quite understand what you need exactly. You state your problem is that you need to use numbers to set permissions? That's not correct, you can also use r w x like this:
Code:
chmod u+rwx file
chmod g+rwx file
chmod a+rwx file
chmod uga+rwx file
and all other possible combinations, where
Code:
u = change permission on user level
g = change permission on group level
a = change permission on all level (world)
+ = add permission
- = remove permission
r = read
w = write
x = execute
Maybe I'm misunderstanding you and you need something else. Can you clarify what you exactly want to do? That way it be easier to provide perhaps a more dedicated answer.

Kind regards,

Eric
 
Old 05-18-2011, 07:49 AM   #3
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Quote:
Change "rwxr-xr-x" to "755"
"rwxr-xr-x" is actually "755" , isn't it ? ?
 
Old 05-18-2011, 07:57 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I think this may be what he is after:
Code:
#!/bin/bash

while read -r DIR
do
    stat -c"%a %U %G %n" "$DIR" >> mydir
done< <(find /home/pingu/test/Cert -type d)
Of course you could probably perform the mkdir in the script as well assuming you have access to serverB.
 
1 members found this post helpful.
Old 05-18-2011, 08:13 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
This bash scrippet converts rwxr-xr-- style strings to numbers:
Code:
#!/bin/bash

buf=$1
numbers_str=
for (( i = 0; i < 3; i++ ))
do
    number=0
    letters=${buf:$(( i * 3 )):3}
    case $letters in
        *r* )
            (( number += 4 ))
            ;;&
        *w* )
            (( number += 2 ))
            ;;&
        *x* )
            (( number += 1 ))
            ;;&
    esac
    number_str=$number_str$number
done
echo $number_str
Other letters are possible in ls output; are you sure the directories only have some combination of r, w, x and -?
 
1 members found this post helpful.
Old 05-18-2011, 08:32 AM   #6
Chirel
Member
 
Registered: Nov 2009
Posts: 55

Rep: Reputation: 19
Hi,

I really don't get it.

Why don't you use the rsync command ?

Code:
rsync -av --include='*/' --exclude='*' source destination
 
5 members found this post helpful.
Old 05-18-2011, 08:51 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Chirel View Post
Hi,

I really don't get it.

Why don't you use the rsync command ?

Code:
rsync -av --include='*/' --exclude='*' source destination
That's good
 
Old 05-20-2011, 03:50 AM   #8
pingu
Senior Member
 
Registered: Jul 2004
Location: Skuttunge SWEDEN
Distribution: Debian preferably
Posts: 1,350

Original Poster
Rep: Reputation: 127Reputation: 127
Chirel, that was awsome!
Can't believe I spent so many hours resolving this and then it turns out to be that easy!

Thanks to all you others too, especially Catkin & Grail who gave me examples of those scripts I've tried desperately to write. Will read them carefully, I still have a lot to learn!

Thanks all!
 
  


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
Fedora Core 2: Screen Resolution can not change from "800X600" to "1024X 768" suhaimi_sj Fedora - Installation 18 12-17-2009 03:29 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
LXer: Displaying "MyComputer", "Trash", "Network Servers" Icons On A GNOME Desktop LXer Syndicated Linux News 0 04-02-2007 08:31 AM
Can you change the "title" under your name that says "member" or "newbie&qu Whitehat LQ Suggestions & Feedback 3 11-19-2003 06:32 PM

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

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