Hot answers tagged

12 votes
Accepted

Executable vs Library in Bash Script

At least when it comes to bash scripts, what exactly is the difference between an executable and a library of bash scripts? I haven't heard the phrase "library" used much for shell scripts, ...
user avatar
  • 119k
11 votes
Accepted

Copy file to same path as source without changing directories

using bash (1), and assuming path without space or new line (2), I would use cp ~/mydir/subdir1/{file1,file2} this would be expanded by bash to (along with ~ expansion) cp ~/mydir/subdir1/file1 ~/...
user avatar
  • 29.9k
11 votes

How to write binary values into a file in Bash instead of ASCII values

perl -e 'print pack "L*", 0..0x7fff' > file Would write them in the local system's endianness. Use: perl -e 'print pack "L>*", 0..0x7fff' perl -e 'print pack "L<*&...
user avatar
9 votes

How can I loop this command in a bash script until the repsonse contains the string "connection successful"

Kusalananda gave tips on how to improve the code, but didn't exactly explain what you did wrong. Let's go over that: I tried something like ret=$? and then if [$ret -ne 0] but I kept getting the else ...
user avatar
  • 4,191
8 votes

Executable vs Library in Bash Script

From bash's point of view there isn't a strict distinction between library and executables. From the Google's style guide, the difference is explained in the section you described. Libraries must ...
user avatar
  • 18.6k
7 votes

Copy file to same path as source without changing directories

With a subshell ( ... ) one can chdir into the desired directory and if that chdir did not fail perform the copy. The chdir only influences the working directory of the subshell, not of the parent ...
user avatar
  • 31.6k
6 votes
Accepted

bash + monitor disk space usage and execute script if used space reached the threshold

You can run df /path/to/directory to get the df output of that directory. For example, on my system: $ df -Ph /home/terdon Filesystem Size Used Avail Use% Mounted on /dev/nvme0n1p6 669G 186G ...
user avatar
  • 219k
5 votes

Executable vs Library in Bash Script

The Google guidelines you reference are a little unclear, but they're talking about the file that contains the routines. They say a file that's written to contain a library of routines must not have ...
user avatar
5 votes
Accepted

How to programmatically get the job id of a newly backgrounded process in Bash

The command jobs prints the currently running background jobs along with their ID: $ for i in {1..3}; do yes > /dev/null & done [1] 3472564 [2] 3472565 [3] 3472566 $ jobs [1] Running ...
user avatar
  • 219k
4 votes

How to write binary values into a file in Bash instead of ASCII values

Use C. This will be slightly faster than the Perl. #include <unistd.h> // in bytes, and MUST be sizeof(int) * somevalue #define BSZ 4096 int main(void) { // TWEAK how far to go up to (set ...
user avatar
  • 31.6k
4 votes
Accepted

AWK return path for files with NF <= 2

Use attr instead of getfattr to retrieve the attribute. This utility will, by default, handle attributes in the user namespace. It is easier to pull out only the attribute's value too, so there's no ...
user avatar
  • 275k
4 votes

How to programmatically get the job id of a newly backgrounded process in Bash

Quoted from the bash manual: There are a number of ways to refer to a job in the shell. The character % introduces a job specification (jobspec). Job number n may be referred to as %n. A job ...
user avatar
  • 803
4 votes

Copy file to same path as source without changing directories

Using find with -execdir (where supported, initially from BSD, but also supported by GNU find) but the specified command is run from the subdirectory containing the matched file For example find ~/...
user avatar
  • 203
4 votes

Bash Run Script on Multiple Files

Your script doesn't take any argument, the names of the INFILE and OUTFILE are hardcoded within. Here, I'd use zsh instead of bash, and do: transform() magick -quiet $1 \ -filter LanczosRadius \ ...
user avatar
4 votes

linux find command + delete all snapshot files except last 5 ordered by date

In zsh: rm -f /zookeeper/version-2/snapshort.*(N.om[6,-1]) Where om orders by modification time (newest first like ls -t does), . restricts to regular files, N enables Nullglob, [6,-1] selects the ...
user avatar
4 votes
Accepted

linux find command + delete all snapshot files except last 5 ordered by date

Remove directly zookeeper snapshots may have unpredictable results for the hadoop cluster. There is specific command for this purpose: zkCleanup.sh -n 5
user avatar
  • 13.9k
3 votes
Accepted

Rename files and folders recursively depth first

find has a -depth option which does exactly what you’re after: find . -depth -iname "*oldName*" -exec rename oldName newName '{}' \;
user avatar
3 votes

Copy file to same path as source without changing directories

Interactively, there's an icp command like imv, that gives you a readline editing environment with the filename. It's in the "renameutils" package. See the man page ...$ icp ~/foo/bar/...
user avatar
  • 6,203
3 votes

Use Git Version of mv/rm etc When in Repository

git mv does a different thing from normal mv and is very much not a drop-in replacement. For instance, it doesn't act on untracked files: touch foo git mv foo bar prints fatal: not under version ...
user avatar
  • 883
3 votes
Accepted

Unable to iterate over all files in bash in archlinux's docker container?

The following line in your script will execute whatever is in $line and append the output to packages_not_in_archlinux.txt: $line >> packages_not_in_archlinux.txt If $line expands to dirmngr, ...
user avatar
  • 275k
3 votes

Podman Ps Format help

Podman in Debian 11 doesn’t process \n in its format templates; they can however be represented as Go strings in the template ({{"\n"}}): podman ps -a --format='ID\t{{.ID}}{{"\n"}}...
user avatar
3 votes
Accepted

No new line when piping into xclip

According to the man page, xclip has a -rmlastnl (-r) option that will do exactly what you want: echo hello | xclip -i -r
user avatar
  • 25.9k
3 votes

Can getopts arguments be combined with other input?

If you're on Linux, and you have the getopt command from util-linux, it can also do the argument reordering similarly to how GNU tools do it. It also supports whitespace in arguments, option-arguments,...
user avatar
  • 119k
2 votes

Can't change Arch default editor

sudo by default sanitizes your environment: variables you set for your user account won't be visible in the process started by sudo. You can run sudo with the -E (--preserve-env) flag: sudo -E visudo ...
user avatar
  • 25.9k
2 votes

Bash, grep - why is a command with a caret not working in a subshell?

(Publishing just because I had all the work to write the question and then noticed my error) There's nothing special about a caret within the string. The command $(git status --porcelain | grep "^...
user avatar
  • 229
2 votes
Accepted

Bash Run Script on Multiple Files

I show another way to solve the problem: to modify the original code using bash. make one single shellscript put a shebang at the top, #!/bin/bash, to control which shell should run the script make a ...
user avatar
  • 5,260
2 votes
Accepted

Getting an error in the if condition

There are a couple of issues in your code: The #!-line should read #!/bin/bash (or whatever the full absolute path is to the bash interpreter or your system). Note that you are using shell code that ...
user avatar
  • 275k
2 votes
Accepted

"ls" sees different files in the same folder on the same machine in two different terminals. How is this even possible?

There are several things that could cause this behavior. Consider this sequence of events: Create a directory, change into it, and create some files: mkdir /tmp/dir1 cd /tmp/dir1 touch file{1,2,3} ...
user avatar
  • 25.9k
2 votes

Can getopts arguments be combined with other input?

This page (Parsing script arguments after getopts) helped me a lot in my quest to understand this. But I'll attempt to explain it all here from the ground up. When anything runs from the terminal, it ...
user avatar
2 votes

Need help on shell script option processing

It's probably most straightforward to just set flags for each of the options, and then after the loop check if the flags for both A and B are set and enable C if they are. But you could model this as ...
user avatar
  • 119k

Only top scored, non community-wiki answers of a minimum length are eligible