!yzBwvLxciGLIBHonQe:matrix.org

bash

224 Members
Discussions about Bash shell, scripting, etc.37 Servers

Load older messages


SenderMessageTime
19 Oct 2021
@kenji_ichikawa:kuvio.deKenji Ichikawa
In reply to rudolff
I utilized a "hack" with grep to highlight words with grep -e '' -e 'word' -the trick it outputs everything because empty string '' matches all lines, but highlights the word in the second pattern.
grep "^\|shit"
22:25:03
@kenji_ichikawa:kuvio.deKenji Ichikawa* `grep "^\|shit"`22:25:16
@rudolff:matrix.orgrudolffThat works, too.22:33:15
21 Oct 2021
@siduck:matrix.orgsiduck joined the room.07:01:17
@siduck:matrix.orgsiduckhi07:01:34
@rudolff:matrix.orgrudolff # hi 11:41:22
@kmatias123:matrix.orgKMatiashi15:25:22
@kyrol86:matrix.orgMichele Campushola15:26:51
@thatoo:defis.infothatooHello22:37:26
@thatoo:defis.infothatooI want to test all files in users directories /home/sftpuser_* and check if there are recent files (less than a day)22:38:53
@thatoo:defis.infothatooI came up with22:38:57
@thatoo:defis.infothatoo * I came up with22:39:06
@thatoo:defis.infothatoo
#!/bin/bash
if [ find /home/sftpuser_* -mtime -1 ]
then
    echo recent enough
fi
22:39:19
@thatoo:defis.infothatoobut I get the following error22:39:40
@thatoo:defis.infothatoo * but I get the following error22:39:55
@thatoo:defis.infothatooline 2: [: too many arguments22:39:57
@thatoo:defis.infothatooif I do22:40:02
@thatoo:defis.infothatoo
#!/bin/bash
if [ find "/home/sftpuser_xxx/onefile" -mtime -1 ]
then
    echo recent enough
fi
22:40:33
@thatoo:defis.infothatooit works for this specific "onefile" file22:41:02
@thatoo:defis.infothatooI found22:52:03
@thatoo:defis.infothatoo
#!/bin/bash
for file in /home/sftpuser_*/*; do
#echo $file
if test `find "$file" -mtime -1`
then
    echo recent enough
fi
done
22:52:16
22 Oct 2021
@mgoerner:matrix.orgmgoernerYour problem is that you put `find` in brackets. In bash, `if` expects commands and checks their exit code. Thus, you invoke the command `[` with argument `find` and I doubt this is what you want.05:38:44
@s0ands0:matrix.orgS0AndS0

thatoo: Ya may be able to use find with a while loop instead of a for loop, eg.

while read -rd '' _file; do
  printf 'Recent file -> %s\n' "${_file}"
done < <(find /home/sftpuser_*/* -type f -mtime -1 -print0)

Alternatively the stat, and date, commands may be useful, eg.

_recent="$(date +%s --date='yesterday')"
for _file in /home/sftpuser_*/*; do
  [[ -f "${_file}" ]] || { continue; }
  [[ "$(stat -c %Y "${_file}")" -gt "${_recent}" ]] && {
    printf 'File smells fresh enough -> %s\n' "${_file}";
    continue;
  }
  # ... do something with older files? ...
done
06:22:14
@s0ands0:matrix.orgS0AndS0 *

thatoo: Ya may be able to use find with a while loop instead of a for loop, eg.

while read -rd '' _file; do
  printf 'Recent file -> %s\n' "${_file}"
done < <(find /home/sftpuser_*/* -type f -mtime -1 -print0)

Alternatively the stat, and date, commands may be useful, eg.

_recent="$(( $(date +%s) - (24 * 60 * 60) ))"

for _file in /home/sftpuser_*/*; do
  [[ -f "${_file}" ]] || { continue; }
  [[ "$(stat -c %Y "${_file}")" -gt "${_recent}" ]] && {
    printf 'File smells fresh enough -> %s\n' "${_file}";
    continue;
  }
  # ... do something with older files? ...
done
06:22:35
@s0ands0:matrix.orgS0AndS0 *

thatoo: Ya may be able to use find with a while loop instead of a for loop, eg.

while read -rd '' _file; do
  printf 'Recent file -> %s\n' "${_file}"
done < <(find /home/sftpuser_*/* -type f -mtime -1 -print0)

Alternatively the stat, and date, commands may be useful, eg.

for _file in /home/sftpuser_*/*; do
  [[ -f "${_file}" ]] || { continue; }
  [[ "$(stat -c %Y "${_file}")" -gt "$(date +%s --date='yesterday')" ]] && {
    printf 'File smells fresh enough -> %s\n' "${_file}";
    continue;
  }
  # ... do something with older files? ...
done
06:25:24
@s0ands0:matrix.orgS0AndS0 *

thatoo: Ya may be able to use find with a while loop instead of a for loop, eg.

while read -rd '' _file; do
  printf 'Recent file -> %s\n' "${_file}"
done < <(find /home/sftpuser_*/* -type f -mtime -1 -print0)

Alternatively the stat, and date, commands may be useful, eg.

_recent="$(date +%s --date='yesterday')"
for _file in /home/sftpuser_*/*; do
  [[ -f "${_file}" ]] || { continue; }
  [[ "$(stat -c %Y "${_file}")" -gt "${_recent}" ]] && {
    printf 'File smells fresh enough -> %s\n' "${_file}";
    continue;
  }
  # ... do something with older files? ...
done
06:26:12
@thatoo:defis.infothatooThank you06:40:38
@mgoerner:matrix.orgmgoernerOne last note: under Linux, the modification timestamp of a folder is always identical to that of the file changed latest. It might be sufficient to just check if your folder was modified less a day ago.07:13:53
@s0ands0:matrix.orgS0AndS0

Happy to help!

If ya go the for loop rout, then there are two shopt options ya may like;

man --pager='less --pattern="^\s+globstar" +n' bash

man --pager='less --pattern="^\s+extglob"' bash

man --pager='less --pattern="^\s+Pathname Expansion"' bash

... which allows for matching a pattern of unknown depth, and known file extension. For example the following will search for paths ending in .css, .html, or .js...

shopt -s extglob globstar

__DIR__="${HOME}/web-site"

for _path in ${__DIR__}/**/*.+(css|html|js); do
  printf '_path -> %s\n' "${_path}"
done

... However, beware that paths expend into a temporary array before the for loop begins execution; so for deep searches find is a safer choice.

17:28:01
@s0ands0:matrix.orgS0AndS0
In reply to @thatoo:defis.info
Thank you
*

Happy to help!

If ya go the for loop rout, then there are two shopt options ya may like;

man --pager='less --pattern="^\s+globstar" +n' bash

man --pager='less --pattern="^\s+extglob"' bash

man --pager='less --pattern="^\s+Pathname Expansion"' bash

... which allows for matching a pattern of unknown depth, and known file extension. For example the following will search for paths ending in .css, .html, or .js...

shopt -s extglob globstar

__DIR__="${HOME}/web-site"

for _path in ${__DIR__}/**/*.+(css|html|js); do
  printf '_path -> %s\n' "${_path}"
done

... However, beware that paths expend into a temporary array before the for loop begins execution; so for deep searches find is a safer choice.

17:29:22

There are no newer messages yet.


Back to Room List