Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results tagged with
Search options not deleted user 40484

Questions about shell scripts, executable files that are interpreted by a shell (bash, zsh, etc.). Check scripts with https://shellcheck.net before posting.

2 votes
Accepted

Find the biggest file in a directory and set the filename in a variable

To restrict the search to the present directory use -maxdepth 1 $ find . -maxdepth 1 -type f -printf "%s\t%p\n" | sort -n | tail -1 5359532 ./coreutils_8.30.orig.tar.xz $ var1="$(basename $(find . - …
Paulo Tomé's user avatar
  • 3,862
0 votes

How to extract a certain string and delete rest of values from an input string?

Here's one way, using a capturing group sed 's/.*TEST3 Id: \(.*\) .*/\1/' Test echo "TEST: www.google.com TEST2: 123444 TEST3 Id: ABCD1234 TEST.txt" | sed 's/.*TEST3 Id: \(.*\) .*/\1/' ABCD1234
Paulo Tomé's user avatar
  • 3,862
11 votes

Ways to append text to a file

Using a here-document approach: cat <<EOF >> file > foo > bar > baz > EOF Tests: $ cat file aaaa bbbb $ cat <<EOF >> file > foo > bar > baz > EOF $ cat file aaaa bbbb foo bar baz
Paulo Tomé's user avatar
  • 3,862
3 votes
2 answers
1k views

Space between line comment character '#' and start of actual comment in shell scripts

There exist shell scripts where comments have a space between the hash sign and the actual comment # a comment at the beginning of a line echo foo # a comment trailing after a command and other tha …
Paulo Tomé's user avatar
  • 3,862
0 votes

How to delete subfolders having only files of a specific extension

The following one liner will solve your problem: sudo find . -links 2 -exec sh -c '[ "$(ls -l \{} | grep -c '^-')" \ -eq "$(ls \{} | grep -c '\.json$')" ]' \; -print0 | xargs -0 echo rm -rf {} + 2> …
Paulo Tomé's user avatar
  • 3,862
0 votes

Is there a way to paste all the arguments in a function in one line?

To exemplify #!/bin/bash g () { for arg in "$@"; do echo "$arg"; done } g 1 2 3 4 Running the script $ ./git.sh 1 2 3 4
Paulo Tomé's user avatar
  • 3,862
1 vote

How can I check if same text appears in two different files

With join, sort and grep join <(sort /path/to/source) <(sort /path/to/destination) | grep '<string to check> Tests cat source a.txt b.txt c.txt d.txt cat destination c.txt a.txt d.txt join <(sor …
Paulo Tomé's user avatar
  • 3,862
1 vote

With inotifywait, exclude files that have certain extensions

Piping your call to grep inotifywait -m -e create --format '%w%f' "${MONITORDIR}" |\ grep '.dat.pgp$' --line-buffered | while read NEWFILE do #decrypts testfile.dat.pgp # new file "testfi …
Paulo Tomé's user avatar
  • 3,862
6 votes

How can I merge 2 files line by line with same record in awk/shell?

This sound like a job for join, the relational database operator join <(sort file1.txt) <(sort file2.txt) Tests $ cat file1.txt Mary 68 Tom 50 Jason 45 Lu 66 $ cat file2.txt Jason 37 Tom 26 Mary …
Paulo Tomé's user avatar
  • 3,862
7 votes
Accepted

Increment build number in bash

awk -F '= ' '/CURRENT_PROJECT_VERSION/{$2=$2+1";"}1' OFS='= ' input > output Tests cat file SOME_DUMMY_VALUE = -1; CURRENT_PROJECT_VERSION = 4; SOME_SECOND_DUMMY_VALUE = -1; CURRENT_PROJECT_VERSION …
Paulo Tomé's user avatar
  • 3,862
2 votes
Accepted

Is there a way to attach file in sendmail command without using uuencode

On Red Hat the uuencode and uudecode commands comes with sharutils package. On Red Hat Enterprise Linux 4, install this package by using up2date command. up2date sharutils On Red Hat Enterprise Li …
Paulo Tomé's user avatar
  • 3,862
3 votes
Accepted

How can I reliably determine if a file is an image file?

The ImageMagick project has facilities to identify images. I'm using ImageMagick all the time, including its "identify" feature from the command line and it never failed once to identify a picture. …
Paulo Tomé's user avatar
  • 3,862
9 votes
Accepted

What is this script doing?

echo 123456789 > out.txt writes the string 123456789 in out.txt file. The exec 3<>out.txt construct opens the file out.txt for reading < and writing > and attaches it to file descriptor #3. read -n …
Paulo Tomé's user avatar
  • 3,862