I’m trying to build a little backup script but I keep getting the following error:
tar: Removing leading `/' from member names tar: /projects: Cannot stat: No such file or directory tar: Exiting with failure status due to previous errors
The folder /projects exists, but still no tar ball is created. Here is my code:
#!/bin/bash backup_files="/projects" #destination of backup dest="/" #Create archive filename day=$(date +%Y-%m-%d) hostname=$(hostname -s) archive_file="$hostname-$day.tar.gz" #Backup the files using tar tar -czf $archive_file $backup_files #Print end status message echo echo "Backup finished"
ls -ld /projects shows the following:
ls: cannot access /projects: No such file or directory
Any idea on what is wrong?
Advertisement
Answer
Filepaths that start with a leading / on Linux and other related systems are located at the root directory. This means that "/projects" usually refers to a different directory than "projects".
It looks like you probably are trying to access a subdirectory /path/to/projects from directory /path/to using the path /projects. This is incorrect — if your working directory is /path/to, you need to access folder projects by changing backup_files="/projects" to backup_files="./projects" — "." refers to the current working directory — or simply backup_files="projects".
So, while relative filepaths "./projects" and "projects" are usually equivalent, they are generally and functionally different from the fully qualified path "/projects".