Bash:从/home/备份所有用户并将其放置到username.tar.gz

Bash:从/home/备份所有用户并将其放置到username.tar.gz,bash,backup,Bash,Backup,我希望从/home/备份所有用户,并为每个用户创建username.tar.gz。如何从/home/for Bash获取用户列表 #!/bin/bash USERS = `ls /home | xargs -r` for user in $USERS; do tar -czf /home/$user.tar.gz /home/$user done 默认情况下,for循环可以扩展通配符*以列出pwd中的所有文件。由于在这里它被命名为/home/*,因此它扩展了/home/目录中的所有

我希望从/home/备份所有用户,并为每个用户创建username.tar.gz。如何从/home/for Bash获取用户列表

#!/bin/bash
USERS = `ls /home | xargs -r`
for user in  $USERS; do
    tar -czf /home/$user.tar.gz /home/$user
done 
默认情况下,for循环可以扩展通配符
*
以列出
pwd
中的所有文件。由于在这里它被命名为
/home/*
,因此它扩展了
/home/
目录中的所有文件和文件夹。因此,对于每个迭代,
文件
/home/

for each in `ls -d /home/*`
do
  # check whether the directory is present in /etc/passwd
  if grep -q $each /etc/passwd
  then
    tar -czf ${each}.tar.gz ${each}
  fi
done

此代码获取
/home
目录中的每个条目,并检查该条目是否存在于
/etc/passwd
文件中。进行此检查是为了防止用户主目录以外的其他文件和目录被tar-ed。

可能/home下的某些文件夹不属于用户,或者用户的home-dir未使用其用户名命名


因此,我认为您最好使用
cat/etc/passwd|grep”/home“{print$1}”awk-F:{print$1}
获取在/home中有home dir的用户,并使用
cat/etc/passwd|grep”/home“{print$6}”awk-F:
获取用户的home dir。

这应该让您开始:

#!/bin/bash

while IFS=: read  user _ uid gid _ home _ ; do
    [[ $home = /home/* && -d $home ]] || continue
    echo "User $user ($uid:$gid) has a home directory: $home"
done < /etc/passwd
#/bin/bash
而IFS=:读取用户uuid gid uuhome;做
[[$home=/home/*&&d$home]| |继续
echo“用户$User($uid:$gid)有一个主目录:$home”
完成
@AvinashRaj:如果mirrol使用LDAP,这将不起作用。谷歌搜索了它?……可能是重复的,或者在脚本顶部添加一些解释!
#!/bin/bash

while IFS=: read  user _ uid gid _ home _ ; do
    [[ $home = /home/* && -d $home ]] || continue
    echo "User $user ($uid:$gid) has a home directory: $home"
done < /etc/passwd