bash: finding file without keyword [duplicate] - bash

This question already has answers here:
Grep : get all file that doesn't have a line that matches [closed]
(3 answers)
Closed 4 years ago.
I am looking for a command in bash that lists the files in which a keyword is not present. for listing files with the keyword I do
fgrep KEYWORD .
I was thinking I could feed vimdiff with two files with the lists, something like this
diff `fgrep KEYWORD .` `ls .` (THIS IS NOT CORRECT)
but I would not like to create two new files at hoc.

How about using simple grep option.
grep -L "foo" *
You could use --files-without-match option too with it.

Related

Can bash be used to rename by pattern? [duplicate]

This question already has answers here:
Rename files using regular expression in linux
(10 answers)
Closed 2 years ago.
I have files:
alpha_123_abc_file.txt
beta_456_def_file.txt
gamma_789_ghi_file.txt
Is there a way to rename all to cut the parts after the first _ character? To become:
123_abc_file.txt
456_def_file.txt
789_ghi_file.txt
I've looked into the perl tool but I an unsure if it has the capability to search out a pattern like that.
for file in *_*; do echo mv -- "$file" "${file#*_}"; done
Remove the echo when you're done testing and ready to actually do the mv.

Issue with bash one liner [duplicate]

This question already has answers here:
Bash function to find newest file matching pattern
(9 answers)
Closed 2 years ago.
I'm trying to do a bash one liner to get the latest logfile to cat and/or tail:
for i in /mnt/usbdrive/backup/filelog_*.log; do ls -t $i | head -n1 ; done
But get all of the matching files:
/mnt/usbdrive/backup/filelog_2020-06-03-09:00:01:345123169.log
/mnt/usbdrive/backup/filelog_2020-06-04-09:00:01:370667894.log
/mnt/usbdrive/backup/filelog_2020-06-04-19:15:27:274135912.log
/mnt/usbdrive/backup/filelog_2020-06-05-09:00:02:020131150.log
/mnt/usbdrive/backup/filelog_2020-06-06-09:00:02:238963148.log
Where am I going wrong?
Also, if I wanted to tail (or cat) that, would I have to declare another variable and tail -f that $variable ?
I'm trying to do a bash one liner to get the latest logfile
You could use
latestfile=$(/bin/ls -t /mnt/usbdrive/backup/filelog_*.log | /bin/tail -1)
assuming you don't have spaces (or semicolons, etc...) in your file names
See ls(1), tail(1) and carefully read the documentation of GNU bash.
You'll better write your script in some other language (e.g. GNU guile, Python, Lua). See the shebang handling of execve(2).
You might also use stat(1) and/or gawk(1) and/or find(1). See glob(7) and path_resolution(7).
You could be interested by logrotate(8) and crontab(5) and inotify(7).

Adding words (tags) to a file [duplicate]

This question already has answers here:
Insert line after match using sed
(8 answers)
Closed 4 years ago.
I want to add the words "Getting started" to two markdown files (so the extension is .md). They are named:
* installing-disqus.md
* installing-google-analytics.md
I would like to populate that word right after the line "Tags: " so the outcome would be "Tags: Getting started"
In Bash, what command would I write. I am thinking it would look something like this:
echo "Getting started" >> *installing* *Tags:*
You could use sed to do a find and replace. Since you want the words added after Tags: (assumming there is only one such line) you could run:
sed -i “s/Tags:/Tags:<your text here>/g” <filename>
-i means that it will do the changes in the file
s/ means it will do a substitution
/
/g do this substitution in the whole file

cut append variable bash [duplicate]

This question already has answers here:
How to extract values from a java properties files in a bash/shell script and store in a variable and cd to that variable
(2 answers)
Code for parsing a key/value in in file from shell script
(6 answers)
Closed 5 years ago.
How can I append a string variable which I got through cut with next variable? In the file application.properties, I have value: myValue=/tmp/user/
I tried:
file=myfile.properties
path=$(cat application.properties | grep myValue=)
path2=$(echo $path | cut -d'=' -f 2- )
pathToFile=$path2$file
But output is only: myfile.properties. I need /tmp/user/myfile.properties
Thank you for your help.
Don't use cut for this at all: The shell's built-in string manipulation can do this trimming for you. (And don't use cat: It's more efficient to have grep read straight from the file than to have it reading from a separate process that then writes to a FIFO).
file=myfile.properies
path=$(grep myValue= <application.properties)
pathToFile=${path#*=}${file}

Bash for loop and glob expansion [duplicate]

This question already has answers here:
Looping on empty directory content in Bash [duplicate]
(2 answers)
Closed 7 years ago.
Consider the following bash code:
for f in /tmp/*.dat; do echo ${f}; done
when I run this and there is no *.dat file in /tmp the output is:
/tmp/*.dat
which is clearly not what I want. However, when there is such a file, it will print out the correct one
/tmp/foo.dat
How can I force the for loop to return 'nothing' when there is no such file in the directory. The find-command is not an option, sorry for that :/ I would like to have also a solution without testing, if *.dat is a file or not. Any solutions so far?
This should work:
shopt -s nullglob
...
From Bash Manual
nullglob
If set, Bash allows filename patterns which match no files to expand
to a null string, rather than themselves.

Resources