I'm trying to gzip a file using a script but it will not work and continues to throw errors. Can someone please giveme some guidance on what is wrong with this script?
DEFAULTDIRECTORY=”/Backup”
if [ -d "$DEFAULTDIRECTORY" ]; then
mkdir -p /backup
fi # Makes directory if the directory does not exist
# Set the timestamp for the backup
TIMESTAMP=`date +%Y%m%d.%H%M`
# let the user choose what they want to backup
echo -n "Select the file or directory you want to backup"
read Chosendata
# read the backup file name file
echo -n "Select the file name"
read FNAME
# start the backup.
echo -e "Starting backup"
# compress the directory and files, direct the tar.gz file to your destination directory
tar -vczf ${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata} > ${DEFAULTDIRECTORY}
# end the backup.
echo -e "Backup complete"
Redirecting output from the tar command to DEFAULTDIRECTORY isn't doing what the comment specifies.
I think what you want to do is save the file in the DEFAULTDIRECTORY.
Change the line
tar -vczf ${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata} > ${DEFAULTDIRECTORY}
to
tar -vczf $DEFAULTDIRECTORY/${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata}
you need to negate this test if [ -d "$DEFAULTDIRECTORY" ]; then -> if [ ! -d "$DEFAULTDIRECTORY" ]; then. You should not use redirect with tar command. instead you should prefix your tar.gz file with it:
tar -vczf $DEFAULTDIRECTORY/${FNAME}-${TIMESTAMP}.tar.gz ${Chosendata}
Try this:
#!/bin/bash
DEFAULTDIRECTORY="/Backup"
# Makes directory if the directory does not exist
# Here you had an inverted statement: use "if [ ! -d ... ] then" or "[ -d ... ] ||"
[ -d "${DEFAULTDIRECTORY}" ] || mkdir -p "${DEFAULTDIRECTORY}"
# Set the timestamp for the backup
# For subshell command you can use `...` or $(...)
TIMESTAMP=$(date +%Y%m%d.%H%M)
# Let the user choose what they want to backup
# You can use the -p (prompt) option instead of using echo
read -p "Select the file or directory you want to backup: " CHOSENDATA
# Read the backup file name
read -p "Select the file name: " FILENAME
# Start the backup.
echo "Starting backup"
# Compress the directory, direct the tar.gz file to your destination directory
# tar 'create' ' zip' 'verbose' 'force' <The output filename> <The data you want to backup>
tar -czvf ${DEFAULTDIRECTORY}/${FILENAME}-${TIMESTAMP}.tar.gz ${CHOSENDATA}
# End the backup.
echo "Backup complete"
Related
I am not able to find a way to make bash create a file with the same name as the file the user dragged into the terminal.
read -p 'file: ' file
if [ "$file" -eq "" ]; then
cd desktop
mkdir
fi
I am trying to make this part of the script take the name of the file they dragged in so for example /Users/admin/Desktop/test.app cd into it copy the "contents" file make another folder with the same name so test.app for this example and then paste the contents file into that folder and delete the old file.
From your .app example, I assume you are using MacOS. Therefore you will need to test this script yourself since I don't have MacOS, but I think it should be doing what you want. Execute it as bash script.sh and it will give you your desired directory test.app/contents in the current working directory.
#! /bin/bash
read -rp 'file: ' file
if [ -e "$file" ]; then
if [ -e "$file"/contents ]; then
base=$(basename "$file")
mkdir "$base"
cp -R "$file"/contents "$base"
rm -rf "$file"
else
echo "The specified file $file has no directory 'contents'."
fi
else
echo "The specified file $file does not exist."
fi
I wrote a bash script to read a bunch of CSV files from a folder and create a backup file in a separate backups directory.
#!/bin/sh
files=$(ls /../test/*.csv 2> /dev/null | wc -l)
if [ **"$files" != "0"** ]
then
# What to backup.
backup_files="/../test/test2"
# Where to backup to.
dest="../test/"
# Create archive filename.
day=$(date +%A)
name="some-file.csv"
archive_file="$name-day.tgz"
# Print start status message.
echo "Backing up $backup_files to $dest/$archive_file"
echo
date
# Backup the files using tar.
tar czf $dest/$archive_file $backup_files
# Print end status message.
echo
echo "Backup finished"
date
else
echo nothing there
break
fi
The script should go through each CSV file (file(1).csv, file(2).csv, etc) and write to my backup file one by one, but the problem is that it only writes from the last file (file(3).csv).
The reason is you are not expanding the $day variable. Use this:
archive_file="$name-$day.tgz"
I'm writing a bash script to send files from a linux server to a remote Windows FTP server.
I would like to check using FTP if the folder where the file will be stored exists before attempting to create it.
Please note that I cannot use SSH nor SCP and I cannot install new scripts on the linux server. Also, for performance issues, I would prefer if checking and creating the folders is done using only one FTP connection.
Here's the function to send the file:
sendFile() {
ftp -n $FTP_HOST <<! >> ${LOCAL_LOG}
quote USER ${FTP_USER}
quote PASS ${FTP_PASS}
binary
$(ftp_mkdir_loop "$FTP_PATH")
put ${FILE_PATH} ${FTP_PATH}/${FILENAME}
bye
!
}
And here's what ftp_mkdir_loop looks like:
ftp_mkdir_loop() {
local r
local a
r="$#"
while [[ "$r" != "$a" ]]; do
a=${r%%/*}
echo "mkdir $a"
echo "cd $a"
r=${r#*/}
done
}
The ftp_mkdir_loop function helps in creating all the folders in $FTP_PATH (Since I cannot do mkdir -p $FTP_PATH through FTP).
Overall my script works but is not "clean"; this is what I'm getting in my log file after the execution of the script (yes, $FTP_PATH is composed of 5 existing directories):
(directory-name) Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
Cannot create a file when that file already exists.
To solve this, do as follows:
To ensure that you only use one FTP connection, you create the input (FTP commands) as an output of a shell script
E.g.
$ cat a.sh
cd /home/test1
mkdir /home/test1/test2
$ ./a.sh | ftp $Your_login_and_server > /your/log 2>&1
To allow the FTP to test if a directory exists, you use the fact that "DIR" command has an option to write to file
# ...continuing a.sh
# In a loop, $CURRENT_DIR is the next subdirectory to check-or-create
echo "DIR $CURRENT_DIR $local_output_file"
sleep 5 # to leave time for the file to be created
if (! -s $local_output_file)
then
echo "mkdir $CURRENT_DIR"
endif
Please note that "-s" test is not necessarily correct - I don't have acccess to ftp now and don't know what the exact output of running DIR on non-existing directory will be - cold be empty file, could be a specific error. If error, you can grep the error text in $local_output_file
Now, wrap the step #2 into a loop over your individual subdirectories in a.sh
#!/bin/bash
FTP_HOST=prep.ai.mit.edu
FTP_USER=anonymous
FTP_PASS=foobar#example.com
DIRECTORY=/foo # /foo does not exist, /pub exists
LOCAL_LOG=/tmp/foo.log
ERROR="Failed to change directory"
ftp -n $FTP_HOST << EOF | tee -a ${LOCAL_LOG} | grep -q "${ERROR}"
quote USER ${FTP_USER}
quote pass ${FTP_PASS}
cd ${DIRECTORY}
EOF
if [[ "${PIPESTATUS[2]}" -eq 1 ]]; then
echo ${DIRECTORY} exists
else
echo ${DIRECTORY} does not exist
fi
Output:
/foo does not exist
If you want to suppress only the messages in ${LOCAL_LOG}:
ftp -n $FTP_HOST <<! | grep -v "Cannot create a file" >> ${LOCAL_LOG}
I want to use a if condition to delete records from the table.
eg :
if ()
then
delete from tablename where filecode like '%A%';
commit;
end if;
the if condition is to check or a particular file format
so there is a possibility of 3 file formats say (A.csv,B.CSV,C.CSV)to arrive in the /a/b/input directory
the column filcode in the table will hold the filename from where the record is obtained.
The above script will run in the location /a/b/scripts directory
Perhaps what you simply want is a script like this. It checks if a given file exists.
#!/bin/sh
file=$1
if [ -e "$file" ]; then
echo "File $file exists."
fi
Or simply
#!/bin/sh
[ -e "$1" ] && echo "File $1 exists."
Save the script in UNIX format in /a/b/scripts like /a/b/scripts/check_file_exists.sh, and run:
sh /a/b/scripts/check_file_exists.sh /a/b/input/somefile.ext
The unix program to check for file formats is called file. To see all the file formats (listed as mime-types) in the directory /a/b/input, run:
file -L --mime-type /a/b/input/*
If you want to select only the files in that directory that are have the jpeg file format, run:
file -L --mime-type /a/b/input/* | grep 'image/jpeg'
try this:
#!/bin/sh
if [ -f /a/b/input ]; then
sh /a/b/scripts/your-script
fi
or this:
test -f /a/b/input && sh /a/b/scripts/your-script
While trying to restore the file "derp" the terminal prints
derp0000644000175000017500000000000512343302711011660 0ustar UserUserDerp
and does not unpack the files , i am confused can someone help me ?
# let the user choose what they want to Restore
echo -n "Select the file or directory you want to Restore"
read chosendata
echo -e "Starting Restore"
# unziping files
gunzip -c ${chosendata}
# end the backup.
echo -e "Restore complete"
"gunzip -c" unzips to stdout rather than to a file. Use "man gunzip" to review options for gunzip.