Need help to write a korn shell script for the below.
Have to write the script in dir ..../script
Have the below files in dir ..../files
Have 2 file patterns
xxx892_1.txt
xxx367_8.txt
xxx356_9.txt
yyy736_9.txt
yyy635_7.txt
Need to get the latest files(last created) matching pattern
xxx and yyy i.e from above xxx356_9.txt, yyy635_7 and ftp them over.
Please need help with this. Thanks.
If by latest you mean time stamp.You can do something like this
ls -t xxx* | head -1 #this will give you the latest modified file
ls -t yyy* | head -1
The above will give you the file Names which you can use for FTP.
Related
I am aware there isn't a special bash function to do this and we will have to build this with available tools -- e.g. sed, awk, grep, etc.
We dump files into a directory and while their filename looks random, they can be mapped to their full description. For example:
/tmp/abcxyz.csv
/tmp/efgwaz.csv
/tmp/mnostu.csv
In filemapping.dat, we have:
abcxyz, customer_records_abcxyz
efgwaz, routernodes_logs_efgwaz
mnostu, products_campaign
We need to go through each of them in the directory recursively and rename the file with its full description. Final outcome:
/tmp/customer_records_abcxyz.csv
/tmp/routernodes_logs_efgwaz.csv
/tmp/products_campaign_mnostu.csv
I found something similar here but not sure how to work it out at directory level dealing with only one file as the lookup/referece file. Please help. Thanks!
I would try something like this:
sed 's/,/.csv/;s/$/.csv/' filemapping.dat | xargs -n2 mv
Either cd to tmp beforehand, or modify the sed command to include the path name.
The sed commands simply replace the comma and the line end with the string ".csv".
I am trying to create a zsh script to test my project. The teacher supplied us with some input files and expected output files. I need to diff the output files from myExecutable with the expected output files.
Question: Does $iF contain a string in the following code or some kind of bash reference to the file?
#!/bin/bash
inputFiles=~/project/tests/input/*
outputFiles=~/project/tests/output
for iF in $inputFiles
do
./myExecutable $iF > $outputFiles/$iF.out
done
Note:
Any tips in fulfilling my objectives would be nice. I am new to shell scripting and I am using the following websites to quickly write the script (since I have to focus on the project development and not wasting time on extra stuff):
Grammar for bash language
Begginer guide for bash
As your code is, $iF contains full path of file as a string.
N.B: Don't use for iF in $inputFiles
use for iF in ~/project/tests/input/* instead. Otherwise your code will fail if path contains spaces or newlines.
If you need to diff the files you can do another for loop on your output files. Grab just the file name with the basename command and then put that all together in a diff and output to a ".diff" file using the ">" operator to redirect standard out.
Then diff each one with the expected file, something like:
expectedOutput=~/<some path here>
diffFiles=~/<some path>
for oF in ~/project/tests/output/* ; do
file=`basename ${oF}`
diff $oF "${expectedOutput}/${file}" > "${diffFiles}/${file}.diff"
done
I have created a test directory structure:
t1.html
t2.php
a/t1.html
a/t2.php
b/t1.html
b/t2.php
All files contain the string "HELLO".
The following commands are run from the root folder above:
> grep -r "HELLO" *
b/t1.html:HELLO
b/t2.php:HELLO
c/t1.html:HELLO
c/t2.php:HELLO
t1.html:HELLO
t2.php:HELLO
> grep -r --include=*.html "HELLO" *
b/t1.html:HELLO
c/t1.html:HELLO
t2.php:HELLO
Why is it including the correct .html files from the sub-directories, but the .php file from the current directory?
If I pop up a level to the directory above my whole structure, then it gives following result:
grep -r --include=*.html "HELLO" *
a/t1.html:HELLO
a/c/t1.html:HELLO
a/b/t1.html:HELLO
This is what I expected when ran from within my structure.
I assume I can achieve the goal using find+grep together, but I thought this was valid usage of grep.
Thanks for any help.
Andy
Use a dot instead of the asterisk:
grep -r HELLO .
Asterisk gets evaluated by the shell and replaced with the list of all the files in the current directory (whose names don't start with a dot). All of them are then grepped recursively.
I want to write a linux script or command that will:
Look into multiple specific directories and list its contents
For example
/test/dir1/abc/version1/program_name/
/test/dir1/abc/version2/program_name/
/test/dir1/abc/version3/program_name/
/test/dir1/bca/version1/program_name/
/test/dir1/bca/version2/program_name/
/test/dir1/bca/version3/program_name/
/test/dir1/cab/version1/program_name/
/test/dir1/cab/version2/program_name/
/test/dir1/cab/version3/program_name/
I can do a
ls -al /test/dir1/*/
and see its contents. But I just want to see what it inside version2 and version3.
for example
ls -al /test/dir1/*/<version2 or version3>/*
and get a list like:
/test/dir1/abc/version2/program_name/
/test/dir1/abc/version3/program_name/
/test/dir1/bca/version2/program_name/
/test/dir1/bca/version3/program_name/
/test/dir1/cab/version2/program_name/
/test/dir1/cab/version3/program_name/
Not including version1. There is more directories than version1, version2, and version3. Thats why just excluding version1 doesnt work.
Any help really appreciated!
You want to use two glob expansions for this search. Try this:
ls -al /test/dir1/*/version[23]/*
It will search through all of the /test/dir1/* directories, and then look for subdirectories matching either 'version2' or 'version3'.
You can use list feature (glob) in BASH:
ls -al /test/dir1/*/{version2,version3}/*
Just wondering how could I less the latest log file in a directory in Linux?
I'm after a oneliner, possibly considering an alias!
Something like this?
ls -1dtr /your/dir/{*,.*} | tail -1 | xargs less
Note that for the first block of ls I am using an answer of Unix ls command: show full path when using options
As it requires a parameter, we create a function instead of an alias. Store the following in ~/.bashrc:
my_less_func ()
{
ls -1dtr "$1"/{*,.*} | tail -1 | xargs less
}
Source it (it is enough doing . ~/.bashrc) and call it with:
my_less_func your/path
In zsh: less dir/*(.om[1])
dir/* is a regular glob.
The . qualifier restricts to regular files.
om means order by modification time, newest first.
[1] means just expand the first filename.
It's probably better without the [1] - just pass all the filenames to less in the om order. If the first one satisfies you, you can hit q and be done with it. If not, the next one is just a :n away, or you can search them all with /*something. If there are too many, om[1,10] will get you 10 newest files.