I am trying to add a tar command to the /test/backup/backup.sh that creates a full backup of everything in my file system except the /text directory to a file called /test/backup.dat. Plus redirect the standard output from this command to a file called /test/backuplog.txt. Redirect the standard error from this command to a file called /test/errlog.txt. How would I get this done? Thank you.
You can do that by adding this at the end of each command
<command> >>/test/backuplog.txt 2>>/test/errlog.txt
also you need to make sure that /test/backuplog.txt and /test/errlog.txt is writable
Related
[dawidn:/mnt/c/Users/dawin]$
This is my default shell directory, all of my projects are on partition d. Is there a way to make my shell start at a certain folder by default?
Edit the file ~/.bashrc
At the end of the file add the line:
cd /mnt/d/.....
Save an close the file.
On logging back into bash, you should have the specified directory as your initial current working directory.
I am trying to create a tar file but not place it in the current working directory but rather the parent of whatever the current working dir is.
I've tried the -C option but with no success.
Did you try like this
$ tar -cvf ../filename.tar ToBeArchivedFolder
Make sure you are using lower case 'c' in the command line options
I'm new to Linux Bash script and learning. I'm just wondering is it possible to redirect stderr to a file only if the stderr contains ERROR.
I am executing Hadoop Hive commands, which I put it in a Bash script file to schedule the process. The hive command generates a lot of logs, and I don't want to redirect the logs to a file every time, But if the log contains Errors, then I want to redirect the log to a file and want to mail the error file to someone.
Please let me know how to achieve this. Thanks in advance..
Regards,
Jeeva
If I understand correctly, if an error occurs, you want to preserve the
entire error log in a file (including lines that might not match your error-detection pattern.). I don't think there's any way to achieve what you
want purely through I/O redirection.
Instead, you can unconditionally redirect stderr to its own file. Then,
as a post-processing step, you can grep through that file to see if
ERROR shows up, and depending on the outcome, either mail the file
to someone, or delete it.
You have to use the stderr file descriptor which is 2
For example:
rm this-file-does-not-exist.txt
>>>> rm: cannot remove ‘asdfasf.js’: No such file or directory
# to redirect that error to a file you do this
rm this-file-does-not-exist.txt 2>/tmp/logError.txt
cat /tmp/logError.txt
>>>> rm: cannot remove ‘asdfasf.js’: No such file or directory
# if you want to check if the output contains `ERROR` do this
badcommand | grep "ERROR" 1>/tmp/logError.txt # if greping for "ERROR" was successfull redirect stdout to /tmp/logError.txt
# the 1 is a file descriptor for stdout
How to use linux mail command
I used command zip in linux (RedHat), this is my command:
zip -r /home/username/folder/compress/zip.zip /home/username/folder/compressed/*
Then, i open file zip.zip, i see architecture as path folder compress.
I want to in folder zip only consist list file *.txt
Because i used this command in script crontab hence i can't use command cd to path folder before run command zip
Please help me
I skimmed the zip man page and this is what I have found. There is not an option archive files relative to a different directory. The closest I have found is zip -j which removes the entire path and stores the files directly in the zip rather than sub directories. I do not know what happens in the case of file name conflicts such as if /home/username/folder/compressed/a.txt and /home/username/folder/compressed/subdir/a.txt both exist. If this is not a problem for you, you can use this option, but I am concerned because you did specify the -r option indicating that you expect zip to traverse sub folders.
I also thought of the possibility that your script could somehow call zip with a different working directory, but I took a look at this unix stack exchange page and it looks like their options use cd.
I have to admit I do not understand why you cannot use cd and I am very curious about it. You said something about using crontab, but I have never heard of anything wrong with changing directories in a crontab script.
I used option -j in command zip
zip -jr /home/username/folder/compress/zip.zip /home/username/folder/compressed/*
and i was yet settled this problem, thanks
So apparently, I can't source a script if that script is in the current directory. For example,
# source some/dir/script.sh
Ok
works fine, but if I'm in the same dir as the script, it errors out:
# cd some/dir
# source script.sh
-sh: source: script.sh: file not found
What gives? Is the only way around this to change directory?
I'm using bash v4.2.10 on Angstrom Linux if that's relevant.
Quoting the source man page:
source filename [arguments]
....
If filename does not contain a slash, file
names in PATH are used to find the directory containing file-
name.
So... source is trying to search your script.sh in the folders contained in PATH.
If you want to source a file in the current folder use
source ./script.sh
Use an absolute path -- source /root/path/to/some/dir/script.sh -- should sort you.
This can happen when the file is in the wrong format. I FTP'd a Korn Shell script from Windows. I could edit it, but got "not found [No such file or directory]" when I tried to run it. It turned out it was in DOS format, which was indicated in the file name line when I edited it in vi. After I re-FTP'd it, making sure it was being transferred as ASCII, it ran fine.