Я только начинаю работать с Ubuntu. Я пытаюсь создать скрипт, который будет делать следующее:
ls -al
для файлов в домашнем каталоге пользователя, а затем сохраните его в файл в моем каталоге. backups
. Проблема, с которой я работаю Я продолжаю получать ошибку «Отказано в разрешении». Он говорит мне, что мой домашний каталог не существует, хотя и должен.
Вот мой скрипт:
#!/bin/bash
cd /home
ls -al > ls.txt
tree/home>tree.txt
mkdir backups
cd backups
mv -t /home/backups ls.txt tree.txt
mv ls.txt ls.bu
mv tree.txt tree.bu
Это вывод:
luse@luse-VirtualBox:~$ sudo 777 script1.sh
sudo: 777: command not found
luse@luse-VirtualBox:~$ chmod 754 script1.sh
luse@luse-VirtualBox:~$ ./script1.sh
./script1.sh: line 4: ls.dat: Permission denied
./script1.sh: line 5: tree.dat: Permission denied
mkdir: cannot create directory `backups': Permissions denied
mv: failed to access '/home/backups': No such file or directory
./script1.sh: line 8: cd: backups: No such file or directory
mv: cannot stat 'ls.dat': No such file or directory
mv: cannot stat 'tree.dat': No such file or directory
luse@luse-VirtualBox:~$
You're doing a few things wrong.
/home/
, not /home
.tree
and the path.mv
command syntax you used to move the .txt
files into the backups
folder must come before cd backups
.Taking all these into account, your script should look like:
#!/bin/bash
cd ~
ls -al > ls.txt
tree ~ > tree.txt
mkdir backups
mv -t ~/backups ls.txt tree.txt
cd backups
mv ls.txt ls.bu
mv tree.txt tree.bu
Notes:
~
is the shorthand for /home/
for the current user, you can also use /home/
in place of ~
to specify the user.backups
folder before running the script again. To overcome this, you need to change the mkdir backups
command to mkdir -p backups
.