Trying to diff two files with generated paths - no such file or directory - but files exist - linux

I want to diff two files in the same directory in a bash script. To get the full paths of these two files (I need this because the script isn't running the same directory), I did:
pathToOld=$(ls -Art /dir/path/here | grep somestring | tail -n2 | head -n1)
pathToOld="/dir/path/here/${pathToOld}"
and
pathToNew=$(ls -Art /dir/path/here | grep somestring | tail -n 1)
pathToNew="/dir/path/here/${pathToNew}"
I was able to figure out the above from the following links: link1, link2, link3
If I echo these path in the .sh script, it comes out correctly, like:
>echo "${pathToOld}"
/dir/path/here/oldFile
But when I try to diff the files like so:
diff pathToOld pathToNew
It tells me:
diff: pathToOld: No such file or directory
diff: pathToNew: No such file or directory
How do I make this work?
Btw, I have also tried to pipe sed -z 's/\n/ /g' (inspired by this) to both lines but that hasn't helped.

Related

Filter directories in piped input

I have a bash command that lists a number of files and directories. I want to remove everything that is not an existing directory. Is there anyway I can do this without creating a script of my own? I.e. I want to use pre-existing programs available in linux.
E.g. Given that I have this folder:
dir1/
dir2/
file.txt
I want to be able to run something like:
echo dir1 dir2 file.txt somethingThatDoesNotExist | xargs [ theCommandIAmLookingFor]
and get
dir1
dir2
It would be better if the command generating the putative paths used a better delimeter, but you might be looking for something like:
... | xargs -n 1 sh -c 'test -d "$0" && echo $0'
You can use this command line using grep -v:
your_command | grep -vxFf <(printf '%s\n' */ | sed 's/.$//') -
This will filter out all the sub-directories in current path from your list.
If in case you want to list only existing directories then remove -v as:
your_command | grep -xFf <(printf '%s\n' */ | sed 's/.$//') -
Note that glob */ prints all sub-directories in current path with a trailing / and sed is used to remove this last /.

Linux commands to get Latest file depending on file name

I am new to linux. I have a folder with many files in it and i need to get the latest file depending on the file name. Example: I have 3 files RAT_20190111.txt RAT_20190212.txt RAT_20190321.txt . I need a linux command to move the latest file here RAT20190321.txt to a specific directory.
If file pattern remains the same then you can try below command :
mv $(ls RAT*|sort -r|head -1) /path/to/directory/
As pointed out by #wwn, there is no need to use sort, Since the files are lexicographically sortable ls should do the job already of sorting them so the command will become :
mv $(ls RAT*|tail -1) /path/to/directory
The following command works.
ls | grep -v '/$' |sort | tail -n 1 | xargs -d '\n' -r mv -- /path/to/directory
The command first splits output of ls with newline. Then sorts it, takes the last file and then it moves this to the required directory.
Hope it helps.
Use the below command
cp ls |tail -n 1 /data...

Ls after cat does not work in Linux

I have a file, containing filepaths, so when I try to list all the path with the following command:
cat whitelist.txt | xargs ls
it displays: No such file or directory.
whitelist.txt contains valid file paths like:
../work/DRA.I3OKGZ.G0200.IB* ../work/DFL.KA6KGZ.G0320.IB*
....
ls works and there are such kinds of files.
So what's the problem?
* does not get expanded.
If you want to keep the "cat | xargs" style, you could do something like
cat whitelist.txt | xargs -I# sh -c "ls #"

Trying to delete lines beginning with a specific string from files where the file meets a target condition, in bash/linux

I am writing a bash script that will run a couple of times a minute. What I would like it to do is find all files in a specified directory that contain a specified string, and search that list of files and delete any line beginning with a different specific string (in this case it's
Here's what I've tried s far, but they aren't working:
ls -1t /the/directory | head -10 | grep -l "qualifying string" * | sed -i '/^<meta/d' *'
ls -1t /the/directory | head -10 | grep -l "qualifying string" * | sed -i '/^<meta/d' /the/directory'
The only reason I added in the head -10 is so that every time the script runs, it will start by only looking at the 10 most recent files. I don't want it to spend a lot of time searching needlessly through the entire directory since it will be going through and removing the line many times a minute.
The script has to be run out of a different directory than the files are in. Also, would the modified date on the files change if the "<meta" string doesn't exist in the file?
There are a variety of problem with this part of the command...
ls -1t /the/directory | head -10 | grep -l "qualifying string" * ...
First, you appear to be trying to pipe the output of ls ... | head -10 into grep, which would cause grep to search for "qualifying string" in the output of ls. Except then you turn around and provide * as a command line argument to grep, causing it to search in all the files, and completely ignoring the ls and head commands.
You probably want to read about the xargs commands, which reads a list of files on stdin and then runs a given command against that list. For example, you ought to be able to generate your file list like this:
ls -1t /the/directory | head -10 |
xargs grep -l "qualifying string"
And to apply sed to those files:
ls -1t /the/directory | head -10 |
xargs grep -l "qualifying string" |
sed -i 's/something/else/g'
Modifying the files with sed will update the modification time on the files.
You can use globbing with the * character to expand file names and loop through the directory.
n=0
for file in /the/directory/*; do
if [ -f "$file" ]; then
grep "qualifying string" "$file" && sed -i '/^<meta/d' "$file"
n=$((n+1))
fi
[ $n -eq 10 ] && break
done

How to: compare command output to text file with bash

I'm trying to compare the output of the command
`ls -l directory`
with a file that I've created using the same command. (I'm trying a poor man's way of making sure no files have been modified.)
The trouble is, I don't know how to diff the output of the ls command with the file that I've created. I've tried the following and each time it doesn't work
diff file.ls <(ls -l directory)
ls -l directory | xargs diff file.ls
ls -l directory | diff file.ls
diff file.ls < `ls -l directory`
What is the magic command to compare the output of ls to a file that I've already saved?
The answer (for posterity) is to do the following
diff file.ls <(ls -l directory)
When I did this previously, the output was blank. I thought I had done it wrong; in actuality there was no difference between the contents of the directory and my file.
<\facepalm>
diff is easiest when you compare files.
$ ls $DIR > original.ls
do some stuff
$ ls $DIR > new.ls
$ diff original.ls new.ls

Resources