listing files in UNIX owned by a particular user - linux

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.

Related

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

ls to list matches in specified directory

How do I list matched results in a specified directory?
On my Ubuntu server if I list the contents of a directory it correctly lists it. My working directory is /var/crash.
#pwd
/var/crash
# ls -l
-rw-r--r-- 1 bob bob 121876 Aug 8 2015 results.xml
-rw-rw-r-- 1 bob bob 126 Nov 3 2015 start.txt
-rw-rw-r-- 1 bob bob 43 Jul 28 2015 exit.txt
Let's say I want to list all files that contain 'tar'. In this example there should only be one match i.e. start.txt
# ls -l *tar*
-rw-rw-r-- 1 bob bob 126 Nov 3 2015 start.txt
All's good so far. However if I include the directory (/var/crash) it lists all files.
# ls -l *tar* /var/crash
-rw-r--r-- 1 bob bob 121876 Aug 8 2015 results.xml
-rw-rw-r-- 1 bob bob 126 Nov 3 2015 start.txt
-rw-rw-r-- 1 bob bob 43 Jul 28 2015 exit.txt
I'm guessing my syntax is telling ls to list all matches of tar AND everything in /var/crash. What is the correct syntax to list matches in a specified directory?
You need to specify the pattern together with the directory:
ls -l /var/crash/*tar*
Otherwise, with ls -l *tar* /var/crash you are telling ls to act against two parameters: /var/crash and *tar*. In fact, *tar* will be expanded before ls reaches it, so there might be more parameters for ls.

Permissions of files within subfolders with SGID in linux

I have a file server based on Ubuntu 14.04. Many users do exist in it where each user belongs to its own group (such as lucas:lucas) but also to a common group called "sambashared".
lucas#arturito:~$ cat /etc/group | grep lucas
adm:x:4:lucas,syslog
lp:x:7:saned,lucas
cdrom:x:24:lucas
sudo:x:27:lucas
dip:x:30:lucas
plugdev:x:46:lucas
lucas:x:1000:
lpadmin:x:111:lucas
sambashare:x:112:lucas
There is also a shared folder under the /home: /home/share. Such a folder has the SGID bit enabled, so files created under it will belong to the "sambashare" group:
lucas#arturito:/home$ ls -l | grep samba
drwxrwsr-x 10 share sambashare 4096 Apr 24 13:44 share
lucas#arturito:/home/share$ touch test.text
lucas#arturito:/home/share$ ls -l test.text
-rw-rw-r-- 1 lucas sambashare 0 Apr 24 14:02 test.text
So, as seen before, the files being created under /home/share are created fine (lucas:sambashare). The issue I'm having is for files being created on a deeper subfolder of /home/share:
lucas#arturito:/home/share/99_varios$ touch file.txt
lucas#arturito:/home/share/99_varios$ ls -l | grep file.txt
-rw-rw-r-- 1 lucas lucas 0 Apr 24 14:19 file.txt
If you see before, the file file.txt belongs to lucas:lucas, but I was hoping it to be lucas:sambashare.
Any idea on how to solve this? Or, is it solvable?
Thanks in advance,
Lucas
Possible workaround is to use facl (file access control lists).
setfacl -m default:group:sambashare:rw /home/samba
All new files in /home/samba and its sub-folders will be owned by lucas:lucas but sambashare group will have read and write permission.
More in getfact (1) and setfact (1).

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.

scp file not setting correct owner

Does SCP have a problem setting file permissions or have I misconfiguration my server?
Use case:
There is a file on a server that I want to edit called "importantFile.txt". The file has owner and group of "master":
ls -l importantFile.txt:
-rw-rw-r-- 1 master master 7 Mar 18 08:11 importantFile.txt
I am called "slave" but luckily, I am in group "master" so I can edit the file as I see fit. However, I'm a lazy slave and can't be bothered to edit the file on the server, I'd prefer to edit the file on my local machine and SCP it to the server:
echo "bored slave info" > importantFile.txt
scp importantFile.txt slave#theServerAddress:/pathToFile/importantFile.txt
If I do this, the contents of the file on the server are uploaded fine and the timestamp of the file is updated but the permissions of the file don't change, the file is still owned by "master". This is a problem because if "slave" uploaded bad content, no one would know it was "slave" who caused the problem, "master" would look guilty.
Perhaps I have to set a umask? if so where? I tried .bash_profile without success and haven't found anything on Google about umask in /etc/ssh/sshd_config.
That's nothing special about scp - try logging on to the server as slave, and editing the file using your favourite text editor... You'll find the same behaviour occurs... Writing to a file does not make you the owner of the file.
Example:
as root
#cd /tmp
#mkdir fubar
#chgrp vboxusers fubar
#cd fubar/
#touch testfile
#chgrp vboxusers testfile
#chmod g+w . testfile
#ls -al
total 16
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 0 2009-03-19 10:30 testfile
#echo foo > testfile
#ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
as user (in vboxusers group)
>cd /tmp/fubar
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
>echo bar >> testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 8 2009-03-19 10:31 testfile
>vim testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:31 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:31 ..
-rw-rw-r-- 1 root vboxusers 12 2009-03-19 10:31 testfile
>cat testfile
foo
bar
baz
You have to delete the file to overwrite it. Wether you are able to do that depends on the directory's permissions and ownership. Hijacking ownership of an already existing file is not possible. The write permission you have is only applied on the contents of the file.
It seems you can configure how Emacs deals with this through the backup-by-copying-when-mismatch variable (see the Emacs Manual or type C-h-v backup-by-copying-when-mismatch in Emacs).
I actually filed a bug report about this, because I thought it was a bug in Tramp.
I had misunderstood the way files work, modifying file contents do not change ownership or group.
Why the confusion? EMACS - Whenever I was editing a file I was using Emacs and Emacs does change the owner and group to the current user. It does this because it makes a backup file at save time by moving the "filename" to "filename~" and creating a new file called "filename" - because it's a new file, it has the current users file permissions. I guess this is 1up to VI fans?

Resources