UNIX:
How to find the number of users who have a given file in their home directory??
As in how can we access the files being used by other users.the command required for that.
I tried find command and all extensions of who
Assuming all the users' home directories are under /home and your're trying to find all users that have a file foo.txt, you can use this find command:
find /home -name "foo.txt" -exec bash -c "IFS=/ && read -a arr <<< {} && echo ${arr[2]}" \;
Assuming you have root privilege and assuming foo.txt is in the home directory, not a subdirectory thereof:
sudo find /home -maxdepth 2 -name "foo.txt" | wc -l
will give you the user count and
sudo find /home -maxdepth 2 -name "foo.txt" -printf "%u\n"
will give you a list of their names (assuming each foo.txt is owned by the owner of the home directory it is found in).
Related
How to find files with a specific pattern in the parent and child directory of my present working directory using a single command ?
Filename - test.txt, the file has the pattern "nslookup"
This file is present in 3 directories and they are /home, /home/1 and /home/1/2
I am currently at /home/1. I have tried below commands :
find ../ -type f -name "test.txt"
Output :
../test.txt
../home/1/test.txt
../home/1/2/test.txt
I was able to find the files, hence I tried the below command :
$ find ../ -type f -exec grep "nslookup" {} \;
nslookup
nslookup
nslookup
This doesn't display the file names.
Command :
find . -type f -name "test.txt | xargs grep "nslookup' ==> gives me files in
pwd and child directories :
./1/test.txt:nslookup
./test.txt:nslookup
but when I try to search in the parent directory as shown below the results are erroneous :
find ../ -type f -name "test.txt" | xargs grep "nslookup
User#User-PC ~/test
$ uname -a
CYGWIN_NT-6.1 User-PC 2.5.2(0.297/5/3) 2016-06-23 14:29 x86_64 Cygwin
How about this:
grep -r -l nslookup .. | grep test.txt
I have a directory structure as below
output/a/1/multipleFiles
output/a/2/multipleFiles
output/a/3/multipleFiles
output/b/1/multipleFiles
output/b/2/multipleFiles
output/b/3/multipleFiles
I want to know number of lines each directory has. So basically, number of lines at each inner most directory level instead of file level. The innermost directories 1, 2, 3 are different kinds of output we generate for our analytics which contains multiple hadoop part-xxxx files.
I moved to output directory and tried the below command.
find . -maxdepth 2 -type d -name '*' | awk -F "/" 'NF==3' | awk '{print $0"/*"}' | xargs wc -l
But I am getting an error as
wc: ./a/1/*: No such file or directory
wc: ./a/2/*: No such file or directory
wc: ./a/3/*: No such file or directory
but if I try
wc -l ./a/1/*
I am getting correct output for that specific folder.
What am I missing here.
EDIT:
I updated my command as below to remove unnecessary awk commands.
find . -mindepth 2 -maxdepth 2 -type d -name '*' | xargs wc -l
This again results in error as
wc: ./a/1: Is a directory
wc: ./a/2: Is a directory
wc: ./a/2: Is a directory
Give a try to execdir, for example:
find . -maxdepth 2 -type f -execdir wc -l {} \;
This will run the command wc -l {} only within the directory that the file has been found, from the man:
-execdir The -execdir primary is identical to the -exec primary with
the exception that utility will be executed from the
directory that holds the current file.
I have this find command and need to make it work recursively in the current directory ( at the moment it searches all the files on the disk )
find . -name ‘OldName*’ -print0 | xargs -0 rename -S ‘OldName’ ‘NewName’
Any idea how to make it search in the current directory that I am navigated to in terminal ?
Would this work?
find . -name 'OldName' -exec rename -n 'OldName' 'NewName' \;
In Unix, is it possible to use one command ONLY to list the directory if a sub-directory exists?
For example, I would like to list the directory name if it contains a sub-directory called "division_A"
/data/data_file/form_100/division_A
/data/data_file/form_101/division_A
/data/data_file/form_102/division_A
The desired result would be
form_100
form_101
form_102
I can only use 2 command lines to realize the goal.
cd /data/data_files
echo `ls -d */division_A 2> /dev/null | sed 's,/division_A,,g'`
So I would like to ask if anyone can use one command to proceed it.
Many Thanks!
Using find:
find /data/data_file -type d -name division_A -exec sh -c 'basename `dirname {}`' \; 2> /dev/null
If you don't mind the weird .., you can just do:
$ ls -d /data/data_file/*/division_A/..
It will output something like /data/data_file/form_100/division_A/.. and you can access it like normal folders.
I'm having a craptastic time trying to figure out how I should configure my Drupal folders and files. I've search all over drupal.org but keep coming up with dribble about the www-data needing access to the "sites" and the "files" folder and how "settings.php" needs some awesome permissions.
But what I need is a list like this:
/ = 744 or drwxr-r--
/includes/ = ...
/misc/ = ...
/modules/ = ...
/profiles/ = ...
/scripts/ = ...
/sites/ = ...
/sites/all/ = ...
/sites/default/ = ...
/sites/default/settings.php = 444?
/sites/default/files/ = ...
I don't think I need someone to catalog every single file, folder, and permission settings for me. I'm guessing that I can just set the root folder permissions to "apply to enclosed items" and then fix the few folders and files that need special settings.
I would really appreciate any contributions that can lead me back to sanity! :)
Scott
default install on my local machine has
-rw-r--r-- all php files
drwxr-xr-x directories
drwxrwxr-x files folder
-r--r--r-- settings.php file
I am quite late for the reply,but I ran into this problem and found a way out.
From Drupal's official handbook:
Copy this into a file and name it as "fix-permissions.sh"
#!/bin/bash
if [ $(id -u) != 0 ]; then
printf "This script must be run as root.\n"
exit 1
fi
drupal_path=${1%/}
drupal_user=${2}
httpd_group="${3:-www-data}"
# Help menu
print_help() {
cat <<-HELP
This script is used to fix permissions of a Drupal installation
you need to provide the following arguments:
1) Path to your Drupal installation.
2) Username of the user that you want to give files/directories ownership.
3) HTTPD group name (defaults to www-data for Apache).
Usage: (sudo) bash ${0##*/} --drupal_path=PATH --drupal_user=USER --httpd_group=GROUP
Example: (sudo) bash ${0##*/} --drupal_path=/usr/local/apache2/htdocs --drupal_user=john --httpd_group=www-data
HELP
exit 0
}
# Parse Command Line Arguments
while [ $# -gt 0 ]; do
case "$1" in
--drupal_path=*)
drupal_path="${1#*=}"
;;
--drupal_user=*)
drupal_user="${1#*=}"
;;
--httpd_group=*)
httpd_group="${1#*=}"
;;
--help) print_help;;
*)
printf "Invalid argument, run --help for valid arguments.\n";
exit 1
esac
shift
done
if [ -z "${drupal_path}" ] || [ ! -d "${drupal_path}/sites" ] || [ ! -f "${drupal_path}/core/modules/system/system.module" ] && [ ! -f "${drupal_path}/modules/system/system.module" ]; then
printf "Please provide a valid Drupal path.\n"
print_help
exit 1
fi
if [ -z "${drupal_user}" ] || [ $(id -un ${drupal_user} 2> /dev/null) != "${drupal_user}" ]; then
printf "Please provide a valid user.\n"
print_help
exit 1
fi
cd $drupal_path
printf "Changing ownership of all contents of "${drupal_path}":\n user => "${drupal_user}" \t group => "${httpd_group}"\n"
chown -R ${drupal_user}:${httpd_group} .
printf "Changing permissions of all directories inside "${drupal_path}" to "rwxr-x---"...\n"
find . -type d -exec chmod u=rwx,g=rx,o= '{}' \;
printf "Changing permissions of all files inside "${drupal_path}" to "rw-r-----"...\n"
find . -type f -exec chmod u=rw,g=r,o= '{}' \;
printf "Changing permissions of "files" directories in "${drupal_path}/sites" to "rwxrwx---"...\n"
cd sites
find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
printf "Changing permissions of all files inside all "files" directories in "${drupal_path}/sites" to "rw-rw----"...\n"
printf "Changing permissions of all directories inside all "files" directories in "${drupal_path}/sites" to "rwxrwx---"...\n"
for x in ./*/files; do
find ${x} -type d -exec chmod ug=rwx,o= '{}' \;
find ${x} -type f -exec chmod ug=rw,o= '{}' \;
done
echo "Done settings proper permissions on files and directories"
Now run this script as:
sudo bash fix-permissions.sh --drupal_path=your/drupal/path --drupal_user=your_user_name
Viola! Your permissions are automatically fixed.
A) It is not advisable to give any form of access to the world, even if it is just read access.
B) To give the owner of the file just a read access leads to complicated maintenance process (eg: most recommended, that Settings.php should be readonly to all), this will only increase your tasks whenever you want to modify the settings.
In nutshell:
- World need 0 access - not even to public folder.
- Your web server needs read only access for all files, except the public folder and tmp folder - these will be both read and write.
- Your file owner needs full access to all files - to keep maintenance simple
This however, will work best when file owner and webserver owner are 2 separate users, and you have ssh control over server and are able to modify the file ownership.
The below script will work when you have following directory structure:
Site Folder
Site Folder/conf (containing apache virtual host configuration files for this site)
Site Folder/htdocs (containing the site)
In this scenario: kalpesh is the file owner and daemon is the webservice owner - it may be www-data for your site.
I normally save such script in a .sh file and then add it to cron, so that whenever my team members upload new content on the site or update a module, the sites permission doesn't get compromised by their mistakes. Cron will execute the scripts and repair permissions every 24 hours.
cd ToSiteFolder
sudo chown kalpesh:daemon .
sudo chmod 750 .
sudo chown -R kalpesh_popat:daemon ./conf
sudo find ./conf -type d -exec chmod 750 {} +
sudo find ./conf -type f -exec chmod 640 {} +
sudo chown -R kalpesh_popat:daemon ./htdocs
sudo find ./htdocs -type d -exec chmod 750 {} +
sudo find ./htdocs -type f -exec chmod 640 {} +
sudo find ./htdocs/sites/default/files -type d -exec chmod 770 {} +
sudo find ./htdocs/sites/default/files -type f -exec chmod 660 {} +
sudo find ./htdocs/tmp -type d -exec chmod 770 {} +
sudo chmod 640 ./htdocs/sites/default/settings.php
sudo chmod 750 ./htdocs/sites/default
There is a blog that explains this beautifully and breaks many myths. https://technologymythbuster.blogspot.com/2018/06/misconception-about-file-ownerships-and.html