- Linux / Unix Find All The Files Owned By a Particular User / Group
- Linux / Unix Find All The Files Owned By a Particular User / Group
- Find file owned by a group
- Find all *.mp4 files by group vivek
- Find file owned by user
- How to find files by users vivek and wendy
- 5 Easy Examples to Delete Files and Directories Owned by Specific User and Group in Linux
- Delete Files and Directories Owned By Specific User and Group in Linux
- Example 1: How to Delete Files Owned by Specific User in Linux
- Example 2: How to Delete Files which belongs to Specific Group in Linux
- Example 3: How to Delete Files owned by Specific User and Specific Group in Linux
- Example 4: How to Delete Directories owned by Specific User and Specific Group in Linux
- Example 5: How to Delete Directories which belongs to Specific Group in Linux
- UNIX / Linux Find File Owner Name
- How do I find out owner / group name for a file?
- ls -l file mode (permissions)
- Linux list processes by user names (EUID and RUID)
- Linux list processes by user names
- How to see process created by a specific user in Linux
- How to show all processes for a specific user using top/htop
- How to display user ID associated with a process
- The pgrep command
- Conclusion
- 40 Best Examples of Find command in Linux
- Find files and Directories
- Find specific files by name or extension
- Looking for specific files in another directory
- Search for files by extension
- Find files and directories by name
- Find files or directories only
- Case insensitive find command
- Search for a file from multiple directories
- Find multiple files with different extensions from all directories
- Find files containing certain text
- Find Files and Directories Based on Size
- Find files of a certain size – equal to 30MB
- Find files larger than a specified size
- Find files less than 10MB in the current directory
- Find files with sizes between 100-200MB
- Look for directories larger than 20kb
- Find empty files and directories.
- Find files by age or modification time
- By modification date
- Find files based on access or modification
- Find files modified within the last n days
- Find files modified within a specific period.
- Files and directories accessed within the last 10 minutes
- Find files matching specific permissions
- Find files with permission 777
- Find files writable by the owner
- Find files owned by a user
- Find specific files owned by a user
- Find and list files and directories together with their permissions
- Find and act on the results
- Find files and change permissions
- Find and change file and directory permissions
- Find and copy files or directories
- Find and copy one file to many directories
- Find and move files to a different directory
- Find certain files and move to a specific different folder
- Find and move files based on age
- Find and delete files and directories
- Find and delete specific files only
- Remove both files and directories
- Delete by extension
- Find and delete files older than n days
- Find and delete directories only
- Find and remove empty files
- Find and remove empty directories
- What’s next?
Linux / Unix Find All The Files Owned By a Particular User / Group
Linux / Unix Find All The Files Owned By a Particular User / Group
Let us see how to use the find command to locate all files/folders owned by one or many users on Linux or Unix-like system.
Find file owned by a group
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | Yes |
Requirements | Linux or Unix |
Est. reading time | 2m |
Use the following syntax to find files owned by users(s) in Linux/Unix:
find directory-location -group < group-name >-name < file-name >
Where,
- directory-location : Locate the file in this directory path.
- -group
: Find the file belongs to group-name. - -name
: The file name or a search pattern
In this example, locate or find all files belongs to a group called “ftpusers” in the /home directory:
# find /home -group ftpusers
To find all *.c file belongs to a group called “ftpusers” in /data/project directory, run:
# find /data/project -group ftpusers -name «*.c»
OR do case insensitive search:
# find /data/project -group ftpusers -iname «*.c»
Find all *.mp4 files by group vivek
Find file owned by user
The syntax is:
find directory-location -user < username >-name < file-name >
Where,
- directory-location : Locate files or directories in this directory location.
- -user < user-name >: Find the file belongs to user.
- -name
: File name or pattern.
In this example, locate or find all file belongs to a user called “vivek” in /var directory:
# find /var -user vivek
To find all *.pl (perl files) file belongs to a user called “vivek” in /var/www directory, enter:
# find /var/www -user vivek -name «*.pl»
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to find files by users vivek and wendy
### match files only ##
# find / -type f -user vivek -o -user wendy
### match dirs only ##
# find / -type d -user vivek -o -user wendy
Conclusion
You just learned how to find all of the files created by a particular user/group and display them to the screen. For more info see find command man page.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
5 Easy Examples to Delete Files and Directories Owned by Specific User and Group in Linux
Table of Contents
In this article, I will take you through 5 Easy Examples to delete Files and Directories Owned by a Specific User and Group in Linux. In Linux based Systems, all the Files and directories are owned by Specific User and belong to some Group. If you are using Linux based Systems from long time, then you might have faced a situation where you need to delete all files and directories based on their ownership.
This task can be easily achieved by using Find command in Linux. You can find this tool to be freely available on almost all the Linux based Systems. I will explain the usage of this command through various examples to delete all the files and directories based on their ownership.
Delete Files and Directories Owned By Specific User and Group in Linux
Example 1: How to Delete Files Owned by Specific User in Linux
If you want to delete files owned by Specific User in Linux then you need to use below find command. In this example, we are deleting all the files owned by User centos using find / -user centos -type f -exec rm -rf <> \; command.
-user : File is owned by user. More information can be checked on find command Man Page.
-type : File is of type block, character, directory, regular file etc. More information can be checked on find command Man Page.
-exec : Execute command; true if 0 status is returned. More information can be checked on find command Man Page.
Example 2: How to Delete Files which belongs to Specific Group in Linux
If you want to delete all the files which belongs to a Particular Group then you need to use below find command. In this example, we are deleting all the files which belongs to group centos using find / -group centos -type f -exec rm -rf <> \; command.
-group : File belongs to group.
Example 3: How to Delete Files owned by Specific User and Specific Group in Linux
If you want to delete files owned by specific user and specific group in Linux then you need to use below find command. In this example we are deleting files owned by User centos and group root using find / -user centos -group root -type f -exec rm -rf <> \; command.
Example 4: How to Delete Directories owned by Specific User and Specific Group in Linux
If you want to delete directories owned by Specific User and Specific Group in Linux then you need to use below find command in Linux. In this example we are deleting directories owned by user centos and group root using find / -type d -user centos -group root -exec rm -rf <> \; command.
Example 5: How to Delete Directories which belongs to Specific Group in Linux
If you want to delete all the directories which belongs to Specific Group then you need to use below find command in Linux. In this example, we are deleting all the directories which belongs to group centos using find / -type d -group centos -exec rm -rf <> \; command.
Popular Recommendations:-
Источник
UNIX / Linux Find File Owner Name
Q. How do I find out the name of file / directory owner under UNIX / Linux operating systems?
A. You can use ls -l command (list information about the FILEs) to find our the file / directory owner and group names.
The -l option is known as long format which displays Unix / Linux / BSD file types, permissions, number of hard links, owner, group, size, date, and filename. In some environments and UNIX versions / Linux distributions, providing the option –color (for GNU ls) or -G (FreeBSD ls) causes ls to highlight different types of files with different colors.
(Fig. 01: Linux file colors)
(Fig. 02: Understanding Linux / UNIX file colors code [ image credit wikipedia] )
How do I find out owner / group name for a file?
Type the ls -l command at a shell prompt:
$ ls -l filename
Sample output:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -rw-r–r– : file mode
- 1 – number of links
- vivek – Owner name (if user name is not a known user, the numeric user id displayed)
- admin – Group name (if group name is not a known group, the numeric group id displayed)
- 2558 – number of bytes in the file (file size)
- Jan 8 07:41 – abbreviated month, day-of-month file was
last modified, hour file last modified, minute file last modified - filename – File name / pathname
ls -l file mode (permissions)
Quoting from the unix ls command man page – the file mode printed under the -l option consists of the entry type and the permissions. The entry type character describes the type of file, as follows:
– | Regular file. |
b | Block special file. |
c | Character special file. |
d | Directory. |
l | Symbolic link. |
p | FIFO. |
s | Socket. |
w | Whiteout. |
The next three fields are three characters each: owner permissions, group permissions, and other permissions. Each field has three character positions:
- If r, the file is readable; if -, it is not readable.
- If w, the file is writable; if -, it is not writable.
- The first of the following that applies:
- S : If in the owner permissions, the file is not executable and set-user-ID mode is set. If in the group permissions, the file is not executable and set-group-ID mode is set.
- s : If in the owner permissions, the file is executable and set-user-ID mode is set. If in the group permissions, the file is executable and set group-ID mode is set.
- x : The file is executable or the directory is searchable.
- – : The file is neither readable, writable, executable, nor set-user-ID nor set-group-ID mode, nor sticky.
- T : The sticky bit is set (mode 1000), but not execute or search permission.
- t : The sticky bit is set (mode 1000), and is search able or executable.
See ls command man page for more information:
$ man ls
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux list processes by user names (EUID and RUID)
Linux list processes by user names
The procedure to view process created by the specific user in Linux is as follows:
- Open the terminal window or app
- To see only the processes owned by a specific user on Linux run: ps -u
- Search for a Linux process by name run: pgrep -u
- Another option to list processes by name is to run either top -U
or htop -u commands
Let us see examples in details to show all processes for a specific user on Linux.
How to see process created by a specific user in Linux
See all process crated by user named tom:
ps -u tom
OR
ps -U tom
EUID is the Effective User ID. The effective user ID describes the user whose file access permissions are used by the process. RUID is the Real User ID. The real user ID identifies the user who created the process. So:
- -u tom : Show all processes by RUID
- -U tom : Display all processes by EUID
You can get a list of every process running as vivek (real [RUID] & effective ID [EUID]) in user format:
ps -U vivek -u vivek
ps -U vivek -u vivek u
## see all process run by, qemu and postfix users ##
ps -U qemu -u qemu
ps -U postfix -u postfix
ps -U postfix -u postfix u
How to show all processes for a specific user using top/htop
The syntax is pretty simple to see all processes created by a user named vivek:
top -U vivek
Linux list processes by user name using top command
Linux show all processes created and used by a user named ‘vivek’ using htop
How to display user ID associated with a process
Another option is to use the combination of ps command and grep command/egrep command:
sudo ps -ef | grep
sudo ps -efl | grep
sudo ps -efl | grep vivek
sudo ps -ef | grep nginx
sudo ps -efl | grep ‘www-data’
The www-data user ID associated with a Linux process named lighttpd, nginx, and php-fpm.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
- -l : Long format
- -a : Show command line args
- -p : Display Linux PIDs
- -s : See parents of the selected process
The pgrep command
The pgrep command can look up processes based on usernames. The syntax is:
### Only match processes whose Linux effective user ID (euid) is listed ###
pgrep -u euid
### Only match processes whose effective user ID (uid) is listed ##
pgrep -U uid
pgrep -l -u vivek
pgrep -l -U www-data
Conclusion
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
40 Best Examples of Find command in Linux
Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
The Linux find command is a powerful tool that enables system administrators to locate and manage files and directories based on a wide range of search criteria. It can find directories and files by their name, their type, or extension, size, permissions, etc.
Besides locating files and directories, combining the find command with others enables you to take action on the results. Adding the -exec option enables sysadmins to run external commands and perform actions like copying, moving, deleting, or changing permissions of the files matching the specified criteria such as size, name, etc.
In this article, we will start by explaining the basic Linux find commands with examples. This will show you how to find files and directories. We will then show you how to use the -exec option to act on the files or directories based on their size, permissions, etc.
The general syntax for the find command is
- path specifies the directory.
- name-of file or dir-to-search : Name of the file or directory to look for
- action-to-take : such as copy, delete, move, etc.
In this tutorial, we will explain how to locate files and directories matching specified patterns. We will also see how to perform actions on the files or directories that the find command locates. The guide is based on Ubuntu but is applicable to most Linux distributions and versions.
Find files and Directories
Find specific files by name or extension
To look for a specific file, run the following command from the root (/). The command contains the exact name for the file you are searching for.
Please note that the results include the path. This is important if you don’t know the directory where the file is located, or when it is in more than one place.
You can also search for the file in another directory while still in the current location. In this case, you need to provide the path for the directory where you want to search.
Looking for specific files in another directory
In our case, we will look for all those starting with the letters file in the test directory.
Search for files by extension
To find a file in Linux with a certain extension, add it to the command.
Find files and directories by name
Use the command below to look for files and directories starting with the letters qa . In our computer, we have the qatree.txt and qa.txt files as well as a directory by the name qa .
If we run the command;
It returns the following output
The command returns both the files and directories matching the search criteria. To find files or directories only, you need to specify this in the command.
Find files or directories only
For files only, use the type f switch.
Files only
Directories only
Add the type d option to locate directories only.
Case insensitive find command
All searches with -name switch are case sensitive and will not give results with capital letters. To get all cases, use the -iname option.
Search for a file from multiple directories
To find the files in different directories, add their paths in the command. In our case, we will check in the test and numeric directories.
Find multiple files with different extensions from all directories
You can use the find command to locate multiple files that share the different extensions such as *.doc , *.txt *.pdf , etc. This can be done separately, one extension at a time, or using just one command that includes all the desired extensions.
find . -type f ( -name «*.txt» -o -name «*.pdf» -o -name «*.doc» )
Find files containing certain text
Sometimes, you want to access a file containing certain text but cannot recall its file name or location. This command allows you to find all the files containing your target text.
To look for all the files containing the word hyperconvergence”, use;
The –i option enables the command to ignore cases and will find the text whether capitalized or not i.e. hyperconvergence, Hyperconvergence , etc.
To look for the files in a specific directory, simply add them to the command
Find Files and Directories Based on Size
You can find all files or directories that are smaller, equal or greater than a certain size, within a certain range or empty. Use the appropriate size format depending on the type of files or directories you are searching for.
Size options include;
Find files of a certain size – equal to 30MB
To Search find all 30MB files
Find files larger than a specified size
Find files less than 10MB in the current directory
Find files with sizes between 100-200MB
When looking for files within a specific range such as between 100 and 200 MB
Look for directories larger than 20kb
find / -type d -size +20k
Find empty files and directories.
Files
find ./ -type f -size 0
Directories
Find files by age or modification time
Find files older than n days
The -mtime +8 will look for txt files that are older than 8 days.
By modification date
This will look for files modified within the last 17 hours
Looks for directories modified within the last 10 days
Find files based on access or modification
Find files based on date or time accessed. This allows you to see files that have or haven’t been accessed within a specified period.
To see files that have not been accessed within the last 10 days in the home directory.
Files accessed exactly 10 days ago
Accessed within the last 10 days
Find files modified within the last n days
You can also look for the files in the /home directory modified within the last 10 days using the command;
Find files modified within a specific period.
For example, all files modified between 6 and 15 days ago in the home directory.
Files and directories accessed within the last 10 minutes
To find the files accessed within the last 10 minutes, use the -amin option.
Directories accessed within the last 10 minutes
Find files matching specific permissions
Where mode is the permission which is either numeric such as 644, 655, 700, 777 , etc, or letters such as u=x, a=r+x, etc.
You can specify the mode in the following three different ways.
- Without a prefix when you want to find files with the exact permissions specified.
- With “ — “ for files with at least the specified permission. This returns files with the specified as well as additional higher permissions.
- Using “ / ” requires specifying the owner or group with the permission to the file.
Find files with permission 777
Find files with at least 766
find -perm -766
The command looks for all files in which the
- The file owner has read/write/execute permissions.
- Group has read/write permissions
- Others have read/write permission
As such, it returns two files that meet this criterion – file1 and file2. The files do not need to have the exact 766 permissions and can have additional ones as long but must have at least the specified.
Find files writable by the owner
We will now use the “ / ” to looks for files writable by either their owner, or group, or others.
The above looks for files that are writable by either their owner or group.
This returns files that are writable by either but not necessarily both. To see files, where both have writable permissions, use the – prefix.
Find files owned by a user
Find all files owned by Jack
Find specific files owned by a user
Find all text files owned by Jack
Find and list files and directories together with their permissions
Find and act on the results
In this section, we will look at how you can act on the files that match the pattern specified in the find command.
Find files and change permissions
Find and change permissions of certain file types. In our case, we will work with PHP files with different permissions as shown below.
We will now look for all the PHP files (above) and replace their permissions with 755
The command looks for PHP files in the ver directory and then sets their permission to 755 ( rwxr-xr-x )
Find and change file and directory permissions
Find files with 644 permissions and change them to have 655 permissions
You can also look for directories with 644 permissions and replace this with 755.
The docs folder has 644 permissions
To set it to 755 , we run
Now we can check again to see what exactly 755
From above we can see the root and docs directories have the 755 permissions.
Ls –la command gives the following details
Find and copy files or directories
Find and copy a specific file to a directory
The command below will find the file22.tx t file and copy it to the
Find and copy one type of files to a directory
To find files such as images with jpg extension in the current directory and copy them to a different location like an images folder, use;
This will find and copy all the jpg files to the
Find and copy one file to many directories
Find and copy a single to multiple directories.
This will find the file hci file and copy it to the three directories of /tmp/dir1/ /tmp/dir2/ and $HOME/3/
Find and move files to a different directory
To move a known file from a directory to another. To move the universal.php file;
Search and move files with a certain extension to a different folder
Find certain files and move to a specific different folder
The command looks for all the files with names starting with uni and having any extension. It then moves them to the directory /unifiles/
Find and move files based on age
Find and move files older than specified days to a different location such as the archive.
This will look for pdf files older than 20 days and move them to the backup1 directory.
Find and delete files and directories
The syntax for finding and removing files or directories in the current directory is
find . -type f -name «file to delete» -exec rm -f <> ; to delete files only or
find . -type d -name «dir-to-delete» -exec rm -rf <> ; to delete directories only
Find and delete specific files only
To find and delete files starting with til, use;
To find and delete directories starting with til
Remove both files and directories
This will remove both files and directories starting with the letters til.
Delete by extension
Below is how you can locate and delete all txt files in the current directory. Replace the txt with another extension such as bak , pdf or any other that you want to remove.
In case you want the system to prompt you to confirm before deleting each file, add the -i option as below.
By default, the -rm will not remove the directories and you need to use the –r option to ensures a recursive removal. This ensures the deletion of empty directories and those containing files. The -f option forces the removal and is used for both the files and directories.
Find and delete files older than n days
Find and delete backup files older than 20 days from the current directory.
This will delete all .bak files older than 20 days.
Find and delete directories only
To delete a directory called dir22
Ensure that you match the directory name case or use the -iname option.
Removes both Dir22 and dir22
To confirm before deletion, use the -i option.
Output
rm: remove directory ‘./Dir22’? n
rm: remove directory ‘./dir22’? y
In our case, we typed n for directory Dir22 which will not be deleted and y for the dir22 which will now be removed.
Find and remove empty files
You can use any of the following commands to locate empty files and delete them automatically.
or
find ./ -type f -size 0 | xargs rm -f
or
find ./ -type f -size 0 –delete
Find and remove empty directories
To remove empty directories we will use the d option.
Another alternative is to use delete instead of remove.
Please note that deleting system or critical files from your computer can damage the operating system or applications or lead to loss of your important data.
To avoid accidental deletions, it is best practice to use a non-root user account. Also, ensure that you are deleting the right files that are not useful and that you have a backup of all your data files just in case.
What’s next?
Go ahead and try the above Linux find commands in your lab or NON PRODUCTION environment. See if you can use some to automate the file system cleanup with Crontab. And, to master Linux, check out this online course.
Источник