showing list of directories using ls - linux

I want to read all home directories of users on a Linux system.
I know I can use:
cut -d':' -f6 /etc/passwd | sort -u
to get a list of those directories.
How can I list each of those directories in detailed format, as when using ls -l?
For example, I would expect to get the root directory as:
dr-xr-x---. 4 root root 4096 Mar 2 02:49 root
Is there any option to pipe the list from the cut command to ls for showing the content?

I think this is what you're looking for:
cut -d':' -f6 /etc/passwd | sort -u | xargs -i% ls -ld %

You can use the following command line:
cut -d':' -f6 /etc/passwd | sort -u | xargs ls -ld

Related

Finding a filetype in current directory and subdirectory, including hidden files. (Homework)

With bash, I am needing to find all gifs in my current directory and subdirectories, and display them in a specific way. I have to include hidden files as well, and I am not allowed to use grep (or its subsidiaries - fgrep and such) or basename. I am running Ubuntu 14.04 through a virtual machine (VirtualBox via Vagrant if that matters) without a GUI. My current script looks like this:
#!/bin/bash
ls -a | find $directory -type f -name "*.gif" | rev | cut -d/ -f1 | rev | cut -d. -f1 | sort -f
This has mostly done what I need, especially with regard to formatting, but when I changed one of the gifs to a hidden file, it was no longer visible - there was an extra empty line, almost as if the file name was written in invisible ink. Does anyone know why it's doing this?
It's not very clear what you are trying to achieve.
As lurker said in the comments cut -d. -f1 will make any line starting with a . be a blank line.
From your code, the closest I could think of is
find $directory -type f -iname '*.gif' | rev | cut -d/ -f1 | cut -d. -f2,3 | rev | sort -f
Giving you all gifs, hidden or not, without path or extension.
Example
user#host /tmp % ls -aR
.:
. .. subdirectory .test.gif test.gif
./subdirectory:
. .. .sub.gif sub.gif
user#host /tmp % find . -type f -iname '*.gif' | rev | cut -d/ -f1 | cut -d. -f2,3 | rev | sort -f
sub
.sub
test
.test

Find all user1 owned and user1's group executable files in the directory in Linux

I want to find all user1 owned and user1's group executable files in the directory in Linux.
Using find following works fine :
find /$mydir -type f -user user1 -perm -010
but need to know how can I do the same thing with ls and grep.
The find command does recursive search if maxdepth is not specified.
For the current directory:
ls -l | awk '$3=="user1"&&$4=="user1group"&&substr($1,1,1)=="-"&&substr($1,7,1)=="x" {print $NF}' will work when you know user1's group.
Or using recursive search among groups in all subfolders:
user="user1";for s in `groups $user| sed 's/.*: //g'`; do awkstmt="ls -R -l | awk '\$3==\"$user\"&&\$4==\""$s"\"&&substr(\$1,1,1)==\"-\"&&substr(\$1,7,1)==\"x\" {print \$NF}'"; eval $awkstmt; done | sort | uniq
Or recursive search among groups in current folder:
user="user1";for s in `groups $user| sed 's/.*: //g'`; do awkstmt="ls -l | awk '\$3==\"$user\"&&\$4==\""$s"\"&&substr(\$1,1,1)==\"-\"&&substr(\$1,7,1)==\"x\" {print \$NF}'"; eval $awkstmt; done | sort | uniq

Problems with ls -l | grep combination

i want to see all files that end with .sh through a ls -l | grep combination. The problem with that is that it has to show only the filename, no other attributes. How do I do this with a ls-l | grep combination?
You can solve this in two ways, one with grep and one without grep:
ls -a | grep "\.sh"
or
ls *.sh

How does the word count program count hidden files?

After creating a few directories and hidden files and running the following commands
ls -al | wc -l
ls -a1 | wc -l
I get a difference in the total returned by the word count program. The
ls -al | wc -l
command returns one more count. Why is this?
$ ls -al | head -n 1
total 57600
This line is not shown with -1.
| is the pipe that connect the command, the output of left command ls -al is the input of right command wc -l.
output of command ls -al is string then wc -l will count the string as a file content,The file names in the string content isn't the argument for command wc -l.
the command xargs is useful,you can use it.
like:
ls -a | xargs wc -l
# find command to find files
find ./* | xargs wc -l

Shell copying file with biggest size to another folder

I am new to Linux shell and I found a way to get the name of the file I want:
ls *.*g -S| grep -v ^d | head -1
I am going to be repeating this for a number of file. I am trying to copy this file to another directory (cp command?). But the below code is failing.
I am trying this, but its not working:
cp ls -S| grep -v ^d | head -1 ../directory
Also, I was wondering how to loop through directorys that are in a particular directory.
Any help is much appreciated.
Thanks,
Bryan
cp $(ls *.*g -S| grep -v ^d | head -1) ../directory
cp "$(find . -type f -name "*.*" -printf "%f:%s\n" | sort -t":" -k2 -n | awk -F":" 'END{print $1}')" ../directory
Use quotes in case there are white spaces in your file. add -maxdepth 1 if you don't want to recurse subdirectories

Resources