Changing permission of a list of file s in a directory meeting certain conditions? - linux

I currently have to change the permission of all the files meeting the criteria g=000 (meaning group has no reading permission, no writing permission and not execution permission) which are in a directory containing ~50 files. I'm with Linux and work on the terminal, using Shell command.
As an example, here are the first 6 (my name have been replaced by user):
-r--r----- 1 user user 0 Sep 23 19:06 fichier_1
-r-x----r- 1 user user 0 Sep 23 19:06 fichier_10
-r--r----- 1 user user 0 Sep 23 19:06 fichier_11
-rwxr----- 1 user user 0 Sep 23 19:06 fichier_12
-r-x---r-- 1 user user 0 Sep 23 19:06 fichier_13
-rw---x--- 1 user user 0 Sep 23 19:06 fichier_14
If they meet the criteria g=---, they need to have all their 'other' permissions removed too, but the user permission have to stay the same. In this case, the files would now look like this (The 2nd and the 5nd files lost all 'others' permissions):
-r--r----- 1 user user 0 Sep 23 19:06 fichier_1
-r-x------ 1 user user 0 Sep 23 19:06 fichier_10
-r--r----- 1 user user 0 Sep 23 19:06 fichier_11
-rwxr----- 1 user user 0 Sep 23 19:06 fichier_12
-r-x------ 1 user user 0 Sep 23 19:06 fichier_13
-rw---x--- 1 user user 0 Sep 23 19:06 fichier_14
I have to only use 1 command, so I can't do them one by one. I tried to use the find and chmod command, but I simply can't seem to make the find command work to find all files with g=--- ( or 000).
My first attempt was this:
$ find /home/mael/tp1_inf1070/World/permissions -type f -perm g=000 -exec chmod o= {} \;
Which gave:
find: invalid mode ‘g=000’
It doesn't work with simply g=--- or just 'g=' either.
If I try to just do the first step,
$ find -perm 'g=000'
I don't get any output.
I could just do:
$ find -perm 700, 600, 400, 707, 704, 706, 707
To find all the files with no group permission, but then how to apply chmod to those files so that all their 'others' permissions get removed, but their user's permission stays the same?
Any idea on how to proceed? Thanks.

Try out to adopt the following to your needs:
find . -not -perm /g=r -and -not -perm /g=w -and -not -perm /g=x -exec chmod o-rwx {} \;
This should find all files, without group permissions and will remove any permissions for world (others).

Related

Change directory using relative path

When I open terminal, my working directory is a/b/c/
and my shell script is placed under a/b/d/e/f/ which gets triggered through Autosys.
I want to change my working directory from a/b/c/ to a/b/d/e/f/ using relative path.
Currently I'm hard coding cd a/b/d/e/f/. I don't want to do it anymore. Could you please let me know how can this be achieved.
cd ../d/e/f/
The trick being the .. which is the "parent directory" of the one you're in
You can see a reference to it (including its permissions) if you use ls -a or more completely
$ ls -lhaF
total 0
drwxr-xr-x 20 user group xxB Jun 15 00:00 ./
drwxr-xr-x 3 user group xxB Jun 15 00:00 ../
-rw-r--r-- 1 user group xxB Jun 15 00:00 e/

listing files in UNIX owned by a particular user

How do I list the files owned by a particular user in UNIX ?.
If I use ls - l command in a shared directory ,it lists all the files with the details .This shared directory contains many files created by many users in a group and I am in a situation where I want to see the files created only by a particular user. Is there any listing command to give username as the input.
Refer below example,
command : ls - l
drwxr-xr-x 2 user_1 main 4.0K Feb 12 16:43 proj_1
drwxrws--- 6 user_2 main 20M Feb 18 11:07 proj_2
drwxr-xr-x 3 user_1 main 1.3M Feb 18 00:18 proj_3
drwxrwsr-x 2 user_2 main 8.0K Dec 27 01:23 proj_4
drwxrwsr-x 2 user_3 main 8.1K Dec 27 01:23 proj_5
I am looking for a command to display only the files created by the user_2 with my expected output as below ,
drwxrws--- 6 user_2 main 20M Feb 18 11:07 proj_2
drwxrwsr-x 2 user_2 main 8.0K Dec 27 01:23 proj_4
Kindly let me know if there is a way .
It should be possible to use awk togheter with ls -l
ls -l | awk '$3=="user_2" { print $0 }'
this will print all lines where third field (user) matches "user_2"
You simply can use the findcommand like this:
find . -maxdepth 1 -user some_user -exec ls -lsad {} \;
Why the options are used:
maxdepth we only want to see current directory level
user we only want to see files owned by given user
exec lets do something with the found file
What we want do with the file:
ls -lsad gives you the long list of current file, if it is a directory, don't go into it.

Is there any way to see all the owners from current directory and file name in huge concurrent logs (without ls)

For my current issue I require listing all the files which have a different owner than the current owner. We have almost 600 000 files in current directory and ls -l or any other ls command will give us trouble.
[system#support forms]$ pwd
/u01/system/reports/foms
[system#support forms]$ ls -ltr|more
total 22066
-rwxrwxrwx 1 system sys 94208 Feb 5 2016 UTIL_COGS.rdf
-rwxrwxrwx 1 system sys 417792 Feb 5 2016 UTIL_AL.rdf
-rwxrwxrwx 1 system sys 212992 Feb 10 2016 UTIL_PE_BATCH.rdf
-rwxrwxrwx 1 system sys 311296 Feb 10 2016 UTIL_GF.rdf
-rwxrwxrwx 1 dev dev 307200 Feb 10 2016 UTIL_NO_ACCT.rdf >>>> this is my Issue
-rwxrwxrwx 1 system sys 1101824 Feb 10 2016 UTIL_NO_DETAIL_REPORT.rdf
-rwxrwxrwx 1 dev dev 614400 Feb 16 2016 UTIL_NO301.rdf >>>> this is my Issue
What do we need to show the files which do not have the expected owner?
You can use the find command:
find . ! -user system
It will show all files not owned by system in the current directory.
You can also choose to see recent files: -mtime -10 will show only files modified less than 10 days ago.
find . -mtime -10 ! -user system
You can also restrict to files only, avoiding to display directories with -type f:
find . -mtime -10 -type f ! -user system

what rights/permissions does a file need to be viewed in a webbroswer

Probably a basic questuion, but, I get the following when I visit a webpage I am working on, so the image is not displayed in the browser.
animacionloading.gif:1 GET http://172.21.200.37/username/dir1/animacionloading.gif 403 (Forbidden)
This is the premissions on the file:
$ls -l dir1/
total 432
-rwx------ 1 username username 359032 Apr 29 08:15 animacionloading.gif
Now I can do a chmod 777 animacionloading.gif which gives me these permissions -rwxrwxrwx 1 username username 359032 Apr 29 08:15 animacionloading.gif
and then I can view the image in the the browser.
My question is what is the minimum rights the file needs to be viewed in the browser?
I have some understanding of the following terms, but just not in this case. Would All Users be anyone that visits the webpage? And would they only need read permissions?
u - Owner
g - Group
o or a - All Users
r - Read
w - Write
x - Execute
EDIT1 for my ref, the following will allow the files be seen in the browser.
$ chmod 004 LoadingBasketContents.gif loop.gif
-------r-- 1 username username 45592 Apr 29 09:05 LoadingBasketContents.gif
-------r-- 1 username username 25465 Apr 29 09:05 loop.gif

Basic Unix refresher inquiry: ls -ld

I know this is really basic, but I cannot find this information
in the ls man page, and need a refresher:
$ ls -ld my.dir
drwxr-xr-x 1 smith users 4096 Oct 29 2011 my.dir
What is the meaning of the number 1 after drwxr-xr-x ?
Does it represent the number of hard links to the direcory my.dir?
I cannot remember. Where can I find this information?
Thanks,
John Goche
I found it on Wikipedia:
duuugggooo (hard link count) owner group size modification_date name
The number is the hard link count.
If you want a more UNIXy solution, type info ls. This gives more detailed information including:
`-l'
`--format=long'
`--format=verbose'
In addition to the name of each file, print the file type, file
mode bits, number of hard links, owner name, group name, size, and
timestamp (*note Formatting file timestamps::), normally the
modification time. Print question marks for information that
cannot be determined.
That is the number of named (hard links) of the file. And I suppose, there is an error here. That must be at least 2 here for a directory.
$ touch file
$ ls -l
total 0
-rw-r--r-- 1 igor igor 0 Jul 15 10:24 file
$ ln file file-link
$ ls -l
total 0
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file-link
$ mkdir a
$ ls -l
total 0
drwxr-xr-x 2 igor igor 40 Jul 15 10:24 a
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file-link
As you can see, as soon as you make a directory, you get 2 at the column.
When you make subdirectories in a directory, the number increases:
$ mkdir a/b
$ ls -ld a
drwxr-xr-x 3 igor igor 60 Jul 15 10:41 a
As you can see the directory has now three names ('a', '.' in it, and '..' in its subdirectory):
$ ls -id a ; cd a; ls -id .; ls -id b/..
39754633 a
39754633 .
39754633 b/..
All these three names point to the same directory (inode 39754633).
Trying to explain why for directory the initial link count value =2.
Pl. see if this helps.
Any file/directory is indentified by an inode.
Number of Hard Links = Number of references to the inode.
When a directory/file is created, one directory entry (of the
form - {myname, myinodenumber}) is created in the parent directory.
This makes the reference count of the inode for that file/directory =1.
Now when a directory is created apart from this the space for directory is also created which by default should be having two directory entries
one for the directory which is created and another for the
parent directory that is two entries of the form {., myinodenumber}
and {.., myparent'sinodenumber}.
Current directory is referred by "." and the parent is referred by ".." .
So when we create a directory the initial number of Links' value = 1+1=2,
since there are two references to myinodenumber. And the parent's number
of link value is increased by 1.

Resources