Finding all directories that are world readable
Rather than parsing ls
, use find
:
find /students -perm -o=r
This would list all files and directories in /students
that are world readable.
If you don't want to recurse into subdirectories and want to find only directories, say:
find /students -maxdepth 1 -type d -perm -o=r
Viewing all directories that are world readable
You can use find -perm
like this:
find /base/path -type d -perm +o+r
+o+r
will only list directories with word (others) read bit on.
Recursively find files that are not publicly readable
Use the find
command:
find . ! -perm -o=r
Will search for files within the current directory and subdirectories that has a file permission so that the "others" group cannot read that file.
The manual page for find
gives some examples of these options.
You can run this command as the www-data
user:
find . ! -readable
To find all files that are NOT readable by the web server.
How to make all files under a directory world readable on linux?
man 3 chmod
contains the information you are looking for.
chmod -R +r directory
the -R
option tells chmod
to operate recursively.
How to find readable folders on linux
"but this command line show me just the readable folders in ( / )
directories and not subdirectories too"
When you set -maxdepth 1
you're restricting the find command to /home
only, remove it to allow find to search recursively.
find /home -type d -perm -o=r
If you need a native php
solution, you can use this glob_recursive
function and is_writable
, i.e.:
<?php
function rglob($pattern, $flags = 0) {
$files = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
}
return $files;
}
$dirs = rglob('/home/*', GLOB_ONLYDIR);
foreach( $dirs as $dir){
if(is_writable($dir)){
echo "$dir is writable.\n";
}
}
Find all writable files in the current directory
find -type f -maxdepth 1 -writable
Finding human-readable files on Unix
find and file are your friends here:
find /dir/to/search -type f -exec sh -c 'file -b {} | grep text &>/dev/null' \; -print
This will find any files (NOTE: it will not find symlinks directories sockets, etc., only regular files) in /dir/to/search and run sh -c 'file -b {} | grep text &>/dev/null' ; which looks at the type of file and looks for text in the description. If this returns true (i.e., text is in the line) then it prints the filename.
NOTE: using the -b flag to file means that the filename is not printed and therefore cannot create any issues with the grep. E.g., without the -b
flag the binary file gettext would erroneously be detected as a textfile.
For example,
root@osdevel-pete# find /bin -exec sh -c 'file -b {} | grep text &>/dev/null' \; -print
/bin/gunzip
/bin/svnshell.sh
/bin/unicode_stop
/bin/unicode_start
/bin/zcat
/bin/redhat_lsb_init
root@osdevel-pete# find /bin -type f -name *text*
/bin/gettext
If you want to look in compressed files use the --uncompress
flag to file. For more information and flags to file see man file.
How to read directories and sub-directories without knowing the directory name in perl?
Your Goal: Find the absolute paths to those directories that do not themselves have child directories.
I'll call those directories of interest terminal directories. Here's the prototype for a function that I believe provides the convenience you are looking for. The function returns its result as a list.
my @list = find_terminal_directories($full_or_partial_path);
And here's an implementation of find_terminal_directories()
. Note that this implementation does not require the use of any global variables. Also note the use of a private helper function that is called recursively.
On my Windows 7 system, for the input directory C:/Perl/lib/Test, I get the output:
== List of Terminal Folders ==
c:/Perl/lib/Test/Builder/IO
c:/Perl/lib/Test/Builder/Tester
c:/Perl/lib/Test/Perl/Critic
== List of Files in each Terminal Folder: ==
c:/Perl/lib/Test/Builder/IO/Scalar.pm
c:/Perl/lib/Test/Builder/Tester/Color.pm
c:/Perl/lib/Test/Perl/Critic/Policy.pm
Implementation
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd qw(abs_path getcwd);
my @dir_list = find_terminal_directories("C:/Perl/lib/Test");
print "== List of Terminal Directories ==\n";
print join("\n", @dir_list), "\n";
print "\n== List of Files in each Terminal Directory: ==\n";
for my $dir (@dir_list) {
for my $file (<"$dir/*">) {
print "$file\n";
open my $fh, '<', $file or die $!;
my $data = <$fh>; # slurp entire file contents into $data
close $fh;
# Now, do something with $data !
}
}
sub find_terminal_directories {
my $rootdir = shift;
my @wanted;
my $cwd = getcwd();
chdir $rootdir;
find_terminal_directories_helper(".", \@wanted);
chdir $cwd;
return @wanted;
}
sub find_terminal_directories_helper {
my ($dir, $wanted) = @_;
return if ! -d $dir;
opendir(my $dh, $dir) or die "open directory error!";
my $count = 0;
foreach my $child (readdir($dh)) {
my $abs_child = abs_path($child);
next if (! -d $child || $child eq "." || $child eq "..");
++$count;
chdir $child;
find_terminal_directories_helper($abs_child, $wanted); # recursion!
chdir "..";
}
push @$wanted, abs_path($dir) if ! $count; # no sub-directories found!
}
Linux: How to list all files/directories, using only ls
Unix utilities were meant to be used as filters, so the answer given makes sense since it best approximates a real-world application.
You do understand, though, that grep "\."
matches everything with a "period" (hence, extension), and grep -v "\."
matches everything else (i.e., the complement).
It is hard to make the command any more precise than what it is already, since who can say what's intended to be an extension, and what's not?
Part 2: ls testfiles | grep "^[^.]*$"
A bracket expression is a list of characters enclosed by [ and ].
It
matches any single character in that list; if the first character of
the list is the caret ^ then it matches any character not in the list.
For example, the regular expression [0123456789] matches any single
digit.
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
So ^[^.]*$
actually means:
Anything that begins and ends with a string of characters, each of which is not a period.
The first caret is not a negation, it means begins with. The second caret is the "not" signifier.
Related Topics
Start Tomcat from Eclipse in Port 80 in Ubuntu with Authbind
Cannot Kill Redis-Server on Linux
How to Decide How Much Stack I Can Use After a Call to Pthread_Attr_Setstacksize
Removing First 3 Characters of File Names in Linux
Why Do My Keystrokes Turn into Crazy Characters After I Dump a Bunch of Binary Data into My Terminal
How to Populate /Dev with a Custom Directory with Udev
Perl-Mechanize Runs into Limitations - Several Debugging Attempts Started
Possible to Assign a New Ip Address on Every Http Request
Why Would Vim Create a New File Every Time You Save a File
Linux Script Telnet Head Request
How to Simulate a Usb Printer to Lpt on Linux
How to Detect Out-Of-Memory Segfaults
Error While Building Pjsip in Linux
Few Shell Commands Doesn't Work When I Invoke a Script via Qprocess in Qt