I am writing a shell script; I want to download latest uploaded file from FTP. I want to get latest file of specific folder. Below is my code for that. But it is not working as expected.
File names are in specific format like
.../MONTHLY_FILE/
ABC_ECI_12082015.ZIP
ABC_ECI_18092015.ZIP
ABC_ECI_09102015.ZIP
Here my return filename should be "ABC_ECI_09102015.ZIP".
Please help me out in this and let me know what mistake I am making.
#!/bin/ksh
. ospenv
#SRC_DIR=/powerm/Myway/SrcFiles
SRC_DIR=$PMRootDir/SrcFiles
cd $SRC_DIR
ftp -n gate.usc.met.com << FINISH
user ftp_abc.com xyz
##Here xyz is password
cd /MONTHLY_FILE
#mget ls -t -r | tail -n 1`enter code here`
get $1
bye
FINISH
I don't understand exactly where exactly you made a mistake, but this should do the trick:
ls -tr | tail -1
what's mget? why do you have an "enter code here"? What I wrote above should print out the last file... whether you save that into a variable, insert it directly to another action, it's up to you.
Related
It's a simple question that I can't seem to figure out. I'm on a Mac with Big Sur with all the latest updates, and I'm going through Terminal to get these commands to run. If there's a better way please let me know.
This is, in basic terms, what I'm trying to do--I want RSYNC to recursively go through a source directory (which in this case would ideally be an entire drive), find any files modified within the last 24 hours, and copy those to another drive, while preserving the folder structure. So if I have:
/Volumes/Drive1/Folder1/File1.file
/Volumes/Drive1/Folder1/File2.file
/Volumes/Drive1/Folder1/File3.file
And File1 has been modified in the last 24 hours, but the other two haven't, I want it to copy that file, so that on the second drive I wind up with:
/Volumes/Drive2/Folder1/File1.file
But without copying File2 and File3.
I've tried a lot of different solutions and strings, but I'm running into problems. The closest I've been able to get is this:
find /Volumes/Drive1/ -type f -mtime -1 -exec cp -a "{}" /Volumes/Drive2/ \;
The problem is that while this one does go through Drive1 and find all the files newer than a day like I want, when it copies them it just dumps them all into the root of Drive2.
This one also seems to come close:
rsync --progress --files-from=<(find /Volumes/Drive1/ -mtime -1 -type f -exec basename {} \;) /Volumes/Drive1/ /Volumes/Drive2/
This one also identifies all the files modified in the last 24 hours, but instead of copying them it gives an error, "link_stat (filename and path) failed: no such file or directory (2)."
I've spent several days trying to figure out what I'm doing wrong but I can't figure it out. Help please!
I think this'll work:
srcDir=/Volumes/Drive1
destDir=/Volumes/Drive2
(cd "$srcDir" && find . -type f -mtime -1 -print0) |
while IFS= read -r -d $'\0' filepath; do
mkdir -p "$(dirname "$destDir/$filepath")"
cp -a "$srcDir/$filepath" "$destDir/$filepath"
done
Explanation:
Using cd "$srcDir"; find . -whatever will generate relative paths (starting with "./") from the source directory to the found files; that means appending the results to $srcDir and $destDir will give the full source and destination paths for each file.
Putting it in parentheses makes it run in a subshell, so the cd won't affect other commands. Coupling cd and find with && means that if cd fails, it won't run find (which would run in the wrong place, generate a list of the wrong file file, and generally cause trouble).
Using -print0 and while IFS= read -r -d $'\0' is a standard weird-filename-safe way of iterating over found files (see BashFAQ #20). Note that if anything in the loop reads from standard input (e.g. cp -i asking for confirmation), it'll steal part of the file list; if this is a worry, use this variant (instead of the pipe) to send the file list over file descriptor #3 instead of standard input:
while IFS= read -r -d $'\0' filepath <&3; do
...
done 3< <(cd "$srcDir" && find . -type f -mtime -1 -print0)
Finally, mkdir -p is used to make sure the destination directory exists, and then cp to copy the file.
I want to save the unix terminal command and out to store in a file for a session. Because some time unix command output are so large, so we con not get back by scrolling up in terminal.
Why not pipe the output to tee ? That will record in a file and dump to the console so you can see in real time what's going on.
$ mycommand | tee filename.log
Note that the above will only record stdout. If you need to record stderr too, then redirect accordingly:
$ mycommand 2>&1 | tee filename.log
(assuming you're using sh or a compatible shell - most likely)
Use the script filename command.
Following html mail using mailx command is working from shell terminal, but the same command is not working from shell script.
mailx -s "$(echo -e "${sub} TRP OF ${system} \nContent-Type: text/html")" example#gmail.com < TRP.html
I guess it is some small escape character error, but not sure what it is.
Can any one help here?
Perhaps your vars sub / system are only known in your current environment.
When your sript is called mymail, try
. mymail
(Start with a dot),
or first export your vars.
When these suggestions fail, debug:
use set -x or temporary put an "echo -e" in front of your line.
Im new to unix,I have search a lot of info but still don not how to make it in a bash
What i know is used this command ls -tr|xargs -i ksh -c "mv {} ../tmp/" to move file by file.
Now I need to make a script that sorts all of these files by system date and moves them into a directory, The first 1000 oldest files being to be moved.
Example files r like these
KPK.AWQ07102011.66.6708.01
KPK.AWQ07102011.68.6708.01
KPK.EER07102011.561.8312.13
KPK.WWS07102011.806.3287.13
-----------This is the script tat i hv been created-------
if [ ! -d /app/RAID/Source_Files/test/testfolder ] then
echo "test directory does not exist!"
mkdir /app/RAID/Source_Files/calvin/testfolder
echo "unused_file directory created!"
fi
echo "Moving xx oldest files to test directory"
ls -tr /app/RAID/Source_Files/test/*.Z|head -1000|xargs -i ksh -c "mv {} /app/RAID/Source_Files/test/testfolder/"
the problem of this script is
1) unix prompt a syntax erro 'if'
2) The move command is working but it create a new filename testfolder instead move to directory testfolder (testfolder alredy been created in this path)
anyone can gv me a hand ? thanks
Could this help?
mv `ls -tr|head -1000` ../tmp/
head -n takes the n first lines of the previous command (here the 1000 oldest files). The backticks allow for the result of ls and head commands to be used as arguments to mv.
How to overcome the case sensitivity while accessing a file in unix using shell script.?
I have a file namely file.txt in the designation path and in script while accessing the same file with the name FILE.txt it throws the error as Cannot open File.
Please guide me how to access the file with case change in unix scripting.
How to turn off case sensitivity in c shell.
file names are case sensitive on Unix systems. As you might have at the same time file.txt and File.txt in the same directory, it is not safe to let a script consider a file name is good when it has not the same case. However, find can tell you:
let my_file="$( find . -iname 'FILE.txt' -maxdepth 1 | head -n 1 )"
(head) ensures you get ONLY one result.
Once again, don't do this, it is EVIL. Change your habits and get used to case sensitivity.
To turn off case sensitivity , in bash, you can use nocaseglob
shopt -s nocaseglob
echo FILE.txt