Display the Files and Folder details under a parent directory in Linux - linux

I have to display all files and folders details under a parent directory.
I am using the command is 'find'. For example,
find /usr/local
/usr/local/bin
It's display only the file name. I have to display file name with details about files like below. Means I have to add below information in the above result set.
-rw-rw-- 1 hduser hduser 213 jan 22 11:51
How to do it?
Thanks in advance.

There's the convenient action -ls:
find /usr/local -ls
If you need some other than the default -ls output format, the action -printf is appropriate; with that you can freely define the format, e. g.:
find /usr/local -printf "%i,%k,%M,%n,%u,%g,%s,%t,%p\n"
Cf. man find: Print File Information.

you can use below command to list it nicely in a order and block wise:-
find . -type d |xargs ls -ltr
For your case:-
find /usr/local -type d |xargs ls -ltr

Try sudo find /usr/local -name "filename" -depth -exec ls -ll {} \;

Related

List all user directories and look for specific file

I'm working on a script which will check for a specific file in ~/ of all users with home directory.
I tried ls /home and cd into users into their home directories but it gives too many arguments error.
username=$(ls /home)
cd /home/$username
cat file.json
I except the output of json file but it doesn't gives output of json file even user have a json file.
Edit:
Now I need to extract username of users with file file.json I have tried to do this with grep but it didn't worked.
files=$(find /home -name tilde.json -print)
echo "$files" >> jsons.txt
cat jsons.txt | grep /*/
This will find and list all files called file.json under the /home directory:
find /home -name file.json -print
You may want to redirect errors to /dev/null in the event you don't have access to all users' home dirs.
If you want to print out the contents of all these files, try:
find /home -name file.json -print -exec cat {} \;
To limit the search to only the directories under /home (i.e. not /home itself, and no sub directories in the user home), use:
find /home -mindepth 2 -maxdepth 2 -type f -name file.json -print -exec cat {} \;
I also added the -type flag there to limit the search to files and exclude any dirs that may happen to share the name.
This'll do:
cat /home/*/file.json
It'll print
cat: '/home/*/file.json': No such file or directory
on standard error if it can't find any.
What about:
cd /home
find . -name file.json -maxdepth 1 -exec cat {} \;
Suppose /home contains user1 and user2.
Then your cd command is invoked as
cd /home/user1 user2
That's not what you wanted, and isn't valid syntax for cd, which accepts only a single argument. You probably wanted a for loop instead.
If you can't predict such expansions, set -x enables tracing, which may provide insight into what commands are actually run, and would show your problem here. set +x to turn it off again.
Finally, note that not all users' home directories are necessarily in home. You might want to use getent or similar to find all user home directories.

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?

How to search (using find command) for directories and copy all the files and directory itself to another directory in linux?
Here is what I have so far:
find -type d -name "*.ABC" -exec {} /Desktop/NewFile \;
I get this as output:
find: './GAE/.ABC: PERMISSION DENIED
Please Help, Thanks!
Your error here above has nothing to do with file read permission. You're trying to execute the directories you find! Avoid running commands as root or sudo unless: (1) you really need it and (2) you really know what you're doing. Quite often people asking for root or sudo privileges are exactly the ones should not have it.
That said... there are several ways to copy a directory tree under *nix. This is just one possible approach:
$ find <start> -type d -name \*.ABC -exec cp -av {} <target> \;
Where:
<start> is a directory name. It's used to tell find where to start its search (for example /usr/local or $HOME)
<target> is another directory name to define the final destination of your copied directories
UPDATE
In case you want to search for multiple paths...
$ find <start> -type d \( -name \*.ABC -o -name \*.DEF \) -exec cp -av {} <target> \;
This should work:
find ./source_dir -name \*.png -print0 | xargs -0 cp -t path/to/destination
For more info, you can look up here.

How to list all the find -perm results?

I want to give a long list (with ls -l) of all the files in home directory that is writable by user, how can I combine find and ls -l?
find ~/ -maxdepth 1 -exec ls -l '{}' \;
If you are strictly interested only in files, i.e., no folders then you can tune the last command in the following way
find ~/ -maxdepth 1 -type f -exec ls -l '{}' \;
Check your man page for "find". It has a -ls action that you can tag on to the end:
-ls True; list current file in ls -dils format on standard output.

Bash list directories that matches pattern, not children of them

I have a folder ~/anna which contains the file ~/anna/b
When I type ls ~/a* I get b.
How can I retrieve ~/anna ?
The script for recreating the scenatrio:
cd ~/
mkdir anna
touch anna/b
ls ~/a*
Expected result: anna
Actually result: b
Thanks!
To get help for the ls, just ask for it:
ls --help
You'll get list of useful options for the ls command, one of them:
-d, --directory list directory entries instead of contents,
and do not dereference symbolic links
So the solution (as stated in comments) would be:
ls -d ~/a*
Depending on your different requirements, find might be more appropriate:
find ~/ -name "a*" -type d
or
find ~/ -mindepth 1 -maxdepth 1 -name "a*" -type d
explanation:
~/: search in home dir
-mindepth 1 -maxdepth 1: only directories "one deep"
-name "a*": all files or folders starting with a
-type d: find only directories

How can I get the owner of every file in a directory in Linux?

I need to check if root is the owner of every file in a particular directory. I can do
stat --format=%u /directory/name/here
to get the owner of the directory itself, but not the files in it.
My other idea was to do
ls -lL | grep "^-\|^d" | cut -d ' ' -f 2
but that doesn't work if the last byte in the permissions is a space and not a '.'.
This is also CentOS if that matters.
you can use find:
find /tmp -type f -printf '%u\n' | sort -u
lightdm
root
tiago
If you need UID in numeric form, like using stat:
find /tmp -type f -printf '%U\n' | sort -u
0
1000
104
You're asking two different questions.
I need to check if root is the owner of every file in a particular directory
To find any files that are not owned by root, you can do:
find /yourdir ! -user root
If it returns any filenames at all, then root is not the owner of every file in the particular directory.
How can I get the owner of every file in a directory in Linux?
To print every file in the directory with username:
find /yourdir -printf '%u %p\n'
And if the final step would be to chown the files not owned by root, you can simply do chown -R root /yourdir, since there's no harm in chowning root's files to root.
Try
find /your/dir/ -type f -exec stat --format='%u %n' '{}' \;
I added %n to display the file name.
Read find(1) for more info about find .... You may want -max_depth 1 to avoid going deeply in /your/dir/
for F in /directory/*; do stat --format='%u' "$F"; done
And optionally add dotglob option to match files beginning with . as well:
shopt -s dotglob
for F in /directory/*; do stat --format='%u' "$F"; done
* --format is equivalent to -c.

Resources