How to print file/directory details using ls and AWK? - linux

I am trying to print the output as below using the command:
ls -l| awk '/777/{print $0}'
or
ls -l| awk '/0777/{print $0}'
But it does not print anything.
O/P should be like,
drwxrwxrwx 2 sbcoper sbcprd 4096 Apr 20 2015 work
(I am using 0777 or 777 because of find . -type f -perm 0777)

you've got the right idea, but 777 is the octal representation of permissions, and with this you're looking at the output of ls, which lists permissions as a string, like "drwxrwxrwx", so a simple modification of your command would work nicely,
ls -l | awk '/rwxrwxrwx/{print $0}'

Related

Display only hidden regular files exluding directories using AWK

I have something that looks like this to display regular files but I don't know how to get it to display only hidden files ".".
ls -al | awk ' /^-/ {print $9}'
.ghost1.c
.ghost2
.ghost3.cpp
input4.txt
lab1.cpp
Lab2.cpp
proc
prog1.c
prog2.c
prog3.c.txt
prog.4c
script1_t03.sh
This is the 9th field and the teacher recommends we use the && operator to display only REGULAR HIDDEN files.
You can use find command for this :
find -maxdepth 1 -type f -name ".*"
The shell expands the pattern .* to all hidden files, including directories and special files, but excluding everything else. Using ls -ld would do the trick, e.g.
ls -ld .* | awk ' /^-/ {print $9}'

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

Proper way to format timestamp in bash

So I need to create an output file for an outside contractor containing a list of filenames from a directory and creation dates in a specific format like this:
FileName YYYYmmDDHHMMSS
So far I've come up with:
find ./ -type f -printf " %f %a\n"
which returns:
FIleName Fri Apr 21 18:21:15.0458585800 2017
Or:
ls -l | awk {'print $9" "$6" "$7" "$8'}
which returns:
FileNAme Apr 21 18:21
But neither is quite the output i need as it needs to be purely numerical and include seconds.
Keeping in mind that the list of files could be very large so efficiency is a priority, how can i get this output?
Something like this
find ./ -type f -printf " %f %AY%Am%Ad%AH%AM%AS\n" |sed -e 's/\.[0-9]*$//'
(sed is needed to remove fractional part after seconds)
(Edit) with ls it will be:
ls -l --time-style=+%Y%m%d%H%M%S |awk {'print $7" "$6'}

shell must parse ls -Al output and get last field (file or directory name) ANY SOLUTION

I must parse ls -Al output and get file or directory name
ls -Al output :
drwxr-xr-x 12 s162103 studs 12 march 28 2012 personal domain
drwxr-xr-x 2 s162103 studs 3 march 28 22:32 public_html
drwxr-xr-x 7 s162103 studs 8 march 28 13:59 WebApplication1
I should use only ls -Al | <something>
for example:
ls -Al | awk '{print $8}'
but this doesn't work because $8 is not name if there's spaces in directory name,it is a part of name. maybe there's some utilities that cut last name or delete anything before? I need to find any solution. Please, help!
EDITED: I know what parse ls -Al is bad idea but I should exactly parse it with construction above! No way to use some thing like this
for f in *; do
somecommand "$f"
done
Don't parse ls -Al, if all you need is the file name.
You can put all file names in an array:
files=( * )
or you can iterate over the files directly:
for f in *; do
echo "$f"
done
If there is something specific from ls that you need, update your question to specify what you need.
How about thisls -Al |awk '{$1=$2=$3=$4=$5=$6=$7=$8="";print $0}'
I know it's a cheap trick but since you don't want to use anything other than ls -Al I cant think anything better...
Based on #squiguy request on comments, I post my comment as an answer:
What about just this?
ls -1A
instead of l (L, the letter), a 1 (one, the number). It will only list the names of the files.
It's also worth noting that find can do what you're looking for:
Everything in this directory, equivalent to ls:
find . -maxdepth 1
Recursively, similar to ls -R:
find .
Only directories in a given directory :
find /path/to/some/dir -maxdepth 1 -type d
md5sum every regular file :
find . -type f -exec md5sum {} \;
Hope awk works for you:
ls -Al | awk 'NR>1{for(i=9;i<NF;i++)printf $i" ";print $i}'
In case you're interested in sed:
ls -Al | sed '1d;s/^\([^ ]* *\)\{8\}//'

Recursively traverse Samba shares?

With bash on linux, how would I write a command to recursively traverse shares mounted, and run commands on each file, to get the file type and size, permissions etc, and then output all of this to a file?
A CIFS share mount would look like a regular directory tree in the linux shell.
The command to search as you need is therefore generic.
From the base directory,
find . -type f -exec ls -lsrt {} \; > file.txt
Ok, this does not give you the file-type detail;
that can be done with a -exec file filename on each file.
mount -v | grep smbfs | awk '{print $3}' | xargs ls -lsR
which you can redirect to a file.
mount -v | awk '/smbfs/{
cmd="ls -lsR "$3
while((cmd | getline d)>0){
print d "->file "$3
}
close(cmd)
}'
find $(mount -t smbfs | awk '{print $3}') -mount -type f -ls -execdir file {} \;
...
33597911 4 -rw-rw-r-- 2 peter peter 5 Dec 6 00:09 ./test.d\ ir/base
./base: ASCII text
3662 4 -rw-rw-r-- 2 peter peter 4 Dec 6 02:26 ./test.txt...
./test.txt...: ASCII text
3661 0 -rw-rw-r-- 2 peter peter 0 Dec 6 02:45 ./foo.txt
./foo.txt: empty
...
If you used -exec file {} +, it would run file once with multiple arguments, but then the output wouldn't be nicely interleaved with find's -ls output. (GNU find's -execdir {} + currently behaves the same as -execdir {} \;, due to a bug workaround. Use -exec file {} \; if you want the full path in the file output as well as in the -ls output above it.
find -ls output is not quite the same as ls -l, since it includes inode an number of blocks as the first two fields.

Resources