- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Check file existence in your directory
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2011 12:06 AM
07-27-2011 12:06 AM
Check file existence in your directory
In the unix server, there shoud have a file which called abc.txt under each user directory , if I want to check if all user direcory have this file , to know which user do not have this file , what can i do ?
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2011 01:47 AM
07-27-2011 01:47 AM
Re: Check file existence in your directory
You can write a small script, that uses the information in /etc/passwd to find the usernames and home directories.
#!/bin/sh USERFILE=abc.txt # from this point on, read /etc/passwd as standard input exec </etc/passwd while read PASSWDLINE; do # username is the 1st field, home directory is 6th USERNAME=$(echo "$PASSWDLINE" | cut -d : -f 1) USERHOME=$(echo "$PASSWDLINE" | cut -d : -f 6) # see if the specified file exists if [ ! -f "$USERHOME/$USERFILE" ]; then echo "$USERNAME does not have the file $USERHOME/$USERFILE" fi done
In general, most "check if condition X is true, for each user" problems in Unix systems can be solved by a general method of "walk through /etc/passwd one line at a time: from each line, pick the user-specific information you need, then perform the check".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2011 05:05 AM
07-27-2011 05:05 AM
Re: Check file existence in your directory
># from this point on, read /etc/passwd as standard input
>exec </etc/passwd
It is probably much easier to use awk than cut(1) and not use tricky stuff like exec:
awk -F: '{print $1, $6}' /etc/passwd | while read USERNAME USERHOME; do
# username is the 1st field, home directory is 6th
# see if the specified file exists
if [ ! -f "$USERHOME/$USERFILE" ]; then
echo "$USERNAME does not have the file $USERHOME/$USERFILE"
fi
done