bash

How to make mv work with exotic directory names?

How to make mv work with exotic directory names? I created several scripts to move messages tagged via notmuch notmuch between Maildir directories. Having multiple accounts, I have some issues with the names of some directories mbsync created when syncing with gmail. Here is my typical Maildir structure for gmail accounts: gmail-user2 ├── [Gmail] │   …

Total answers: 1

Shell: How to print all files in a folder with cat with files separator

Shell: How to print all files in a folder with cat with files separator If I do cat * in a directory, then I cannot easily see when I switch from one file to the next. I would like to have a visual indication that the files are not the same. I tried options -n …

Total answers: 1

help with bash script to [grep sed cut] smartctl output to desired format

help with bash script to [grep sed cut] smartctl output to desired format If anyone is good at writing bash scripts, and is inclined to do the following, I am looking to achieve this: i am horrible at writing bash and more horrible at using cut or sed, and if this can be done with …

Total answers: 3

Append the arguments to a script as lines in a file

Append the arguments to a script as lines in a file How do you append $@ or $* , each as a new line to a file? I think I already searched for that before. Adding a new option -i –insert-newline with possible values 1, 2, 3 or Linux, Windows, Mac to echo command should …

Total answers: 1

how to de-duplicate block (timestamp+command) from bash history?

how to de-duplicate block (timestamp+command) from bash history? I’m working with bash_history file containing blocks with the following format: #unixtimestampncommandn here’s sample of the bash_history file: #1713308636 cat > ./initramfs/init << "EOF" #!/bin/sh /bin/sh EOF #1713308642 file initramfs/init #1713308686 cpio -v -t -F init.cpio #1713308689 cpio -v -t -F init.cpio #1713308690 ls #1713308691 ls My …

Total answers: 2

How to make bc handle explicitly positive numbers?

How to make bc handle explicitly positive numbers? Consider the following: $ echo "-1" | bc -1 $ echo "(1)" | bc 1 $ echo "+1" | bc (standard_in) 1: syntax error Minus sign and round braces are handled correctly but when explicit plus sign is specified bc falls with error. Is it possible to …

Total answers: 3

how to replace the last n occurences of string using sed

how to replace the last n occurences of string using sed I can replace the third occurrence of a string with: sed ‘s/OLD/NEW/3’ or, with the GNU implementation of sed, all occurrences starting from the third with: sed ‘s/OLD/NEW/3g’ But how do I replace the last 3 (or n) occurrences? Asked By: dhm || Source …

Total answers: 4

Diagnose bash autocomplete issues

Diagnose bash autocomplete issues I have one binary on my system, fio installed via package manager which doesn’t autocomplete files with tab once you’ve typed fio . I guess this means something must over overridding the autocomplete behavior but I don’t even know how to begin diagnosing it. Asked By: BeeOnRope || Source I would …

Total answers: 1

Setting PS1 with site-wide configuration

Setting PS1 with site-wide configuration I’ve had problems with people logging into some servers via ssh and forgetting that a specific terminal is no longer local. Today someone tried to shutdown their laptop with sudo shutdown -h now, and accidentally took down a server. I’d like to change the colour of bash’s PS1 on these …

Total answers: 3

printf – store formatted string output in a variable

printf – store formatted string output in a variable Instead of the following printf ‘%st%st%st%sn’ ‘index’ ‘podcast’ ‘website’ ‘YouTube’ I want to store the printf output in a Results variable, how can I do this? Asked By: Porcupine || Source Bash (since 3.1), zsh (since 5.3) and ksh93 (since v- and u+m 2021-11-28) have an …

Total answers: 1

Use a here-document to create a script for AWK

Use a here-document to create a script for AWK Why doesn’t this work? #!/bin/bash DOMAIN="example.com" PING_COUNT=3 #prints column header awk ‘BEGIN {printf("%-8s %-8s %-8s %-8s %sn", "avg", "min", "max", "mdev", "hostname")}’ FORMAT_RESULTS=$(cat << EOF ‘{ split($0, line, " = ") split(line[1], temp) hostname = temp[2] split(line[2], temp) split(temp[1], stats, "/") min = stats[1] avg = …

Total answers: 2

Sorting Bash History for Redundancy Removal

Sorting Bash History for Redundancy Removal I’m working with my .bash_history file and want to identify repetitive commands for cleanup. Here’s a sample snippet: … #1713251479 sh lowbat.sh #1713251495 nvim lowbat.sh #1713252186 sh lowbat.sh #1713253121 xclip -sel clip lowbat.sh #1713253722 cd code/dotfiles/ #1713253766 git add mpv/ … My goal is to: Sort the commands alphabetically …

Total answers: 1

how to columnate list of strings in two lines, aligned to the length of the first column

how to columnate list of strings in two lines, aligned to the length of the first column I have a list of strings: python2-distutils-extra|==>|python2-distutils-extra|2.39-5|Enhancements to the Python build system python2-fuse|==>|python2-fuse|1.0.5-1|This is a Python 2 interface to libfuse (https://github.com/libfuse/libfuse), a simple interface for userspace programs to export a virtual filesystem to the Linux kernel python2-gobject2|==>|python2-gobject2|2.28.7-7|Legacy Python …

Total answers: 2

AWK: regex is not processed when passed as a variable

AWK: regex is not processed when passed as a variable Any suggestions? I read: Escape Sequences (The GNU Awk User’s Guide) Representative Example $ Filesystem=’/dev/mapper/vgubuntu-Media’ $ FilesystemRegex="$(echo "${Filesystem}" | sed "s~/~\/~g")" $ echo "$FilesystemRegex" /dev/mapper/vgubuntu-Media $ df -H "${Filesystem}" | awk -v Regex="${FilesystemRegex}" ‘/$Regex/{print $5}’ awk: warning: escape sequence `/’ treated as plain `/’ $ …

Total answers: 2

How do I get bash to use a specific directory when running a command?

How do I get bash to use a specific directory when running a command? I have a problem with the handling of the mysql command.  When I try to use mysql -uroot -p, I get an error: Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2) However, when I try to use ./mysql -uroot -p from /data/local/tool/mysql-5.6.21/bin, I can connect to …

Total answers: 2

`command echo –help` invokes bash internal echo function

`command echo –help` invokes bash internal echo function According to help command, the help of bash, command can be used to trigger the external program x, if x exists as a builtin function too: command: command [-pVv] command [arg …] Execute a simple command or display information about commands. Runs COMMAND with ARGS suppressing shell …

Total answers: 1

pipe to a different file descriptor

pipe to a different file descriptor Following up on Using while loop to ssh to multiple servers, i.e., for while IFS= read -r -u9 HOST ; do ssh "$HOST" "uname -a" ; done 9< servers.txt which reads from a different file descriptor (9), How to make it read from a pipe of a different file …

Total answers: 1

bash/sed replace string of repeating string within a string

bash/sed replace string of repeating string within a string I have a file where each line represents data that may be encompassed with a string like "o o ". I need to replace the string between the encompassing characters with another string. The "internal" string is a pattern of two characters that may include an …

Total answers: 1

Remove /dev mount point without unmount in Host

Remove /dev mount point without unmount in Host Here is the unmount method for unmounting all previously mounted folders: https://unix.stackexchange.com/questions/61885/how-to-unmount-a-formerly-chrootd-filesystem I use the following command to mount the /sys and /dev to the chroot from the Ubuntu host: cd /path/to/chroot/ mount -t sysfs /sys sys mount -o bind /dev dev If I delete the /sys …

Total answers: 1

How can I add a property in the end of the object (text manipulation)

How can I add a property in the end of the object (text manipulation) I’m doing some text manipulation and I want to add a property (at the end) to an object named "object" that is inside a configuration file e.g . . object = { "one": "one", "two": "two", } . . Should become …

Total answers: 3