Tag Info

New answers tagged

0

I have found that the fastest and most memory-efficient solution is to use this command in the directory full with millions of files: ls -f1 | xargs rm Explanation Because ls -f1 will not sort the directory content, it will start the output right away. The rm command will take just one argument: the actual file name coming from the first command. It ...


0

Recently had this issue myself, because these solutions require you to re-enter the password every time if you use a password login I used sshpass in a loop along with a text prompt to avoid having the password in the batch file. Thought I'd share my solution on this thead in case anyone else has the same issue: #!/bin/bash read -s -p "Password: " pass ...


1

It is possible that I misunderstood you, but it seems that you are working with a POSIX shell. You can do it with awk (gawk). As far as each empty directory contains only 2 files by default (. and ..), you can use [ $(wc -l <<< $(ls -as $DIR)) -ne 2 ] as folder emptiness condition. $ find . -type d | awk '{print "[ $(wc -l <<< $(ls -a \"" ...


1

You can use the -C flag to enable compression in scp transfer. This should be enough, although you can check man scp for more details on compression.


2

gzip will write to STDOUT, and scp can't handle it. try gzip -c aum.dmp | ssh -l oracle 192.168.0.191 'cat > /export/home/oracle/aum.dmp.gz' instead. where gzip -c aum.dmp | will gzip aum.dmp, and send result to stdout ssh -l oracle 192.168.0.191 will connect to user oracle on 192.168.0.191 'cat > /export/home/oracle/aum.dmp.gz' will execute ...


0

Print terminal output to file, rather than the terminal your currently in: commands >>outputfile In new terminal, use tail to show contents of outputfile and update it automatically as more text is added: tail -f outputfile


0

If I understand it right you want to understand how to make this more short and structured in Shell language. I would do something like this: set -A strings \ One \ Two \ Three \ Four # You should swap Three and Four so it's easy to fit the logic. counter=0 for i in \ "[ \"$NEARESTSERVER\" == 'SRV-MUMBAI.xaas.com' ]" \ "[ ...


1

There are so many things wrong with your script, that it is difficult to know where to begin. First, the syntax needs end-of-line markers: for i in *; do mv $* $script_'$i'; done Now the mv parameter 1 needs to be the name of the current file: for i in *; do mv $i $script_'$i'; done Next the mv parameter 2 has three errors ($script_ expands the ...


0

perl-rename is well suited for this: $ cd -- "$(mktemp --directory)" $ mkdir foo bar baz $ perl-rename --dry-run --verbose 's/.*/sprintf "script_%04d", ++$main::Mad/e' ./*/ ./bar/ -> script_0001 ./baz/ -> script_0002 ./foo/ -> script_0003


1

The problem is that the shell is seeing the parentheses in $RELEASE outside of any quotes, so is trying to interpret them. Two things to try: put double-quotes around the reference to $(RELEASE), and use := per this. For example: SHELL:=/bin/bash. #at the top of the makefile IS_FEDORA22_i686 := $(shell echo "$(RELEASE)" | $(EGREP) -i -c "fc22.i686") ...


0

Remove the .phar extension and disregard making the bat file if you're using an editor that isn't command prompt. In the last line on the Windows installation documentation for phpunit it reads: For Cygwin and/or MingW32 (e.g., TortoiseGit) shell environments, you may skip step 5. above, simply save the file as phpunit (without .phar extension), and ...


0

cat will strip out some characters depending on your current terminal settings. DD will preserve the file content as-is, as will cp command.


0

The ssh -L syntax with the ports you've listed would be: ssh -L 8899:<Host B>:22 user@<Host A> This would listen on port 8899 on , and anything it receives will be forwarded via ssh to and from there sent to in the clear. will think it's communicating with something running on I'm not sure how this would help you. You can chain up ssh ...


0

I found this perfect answer on the fish-users mailing list: function bind_bang switch (commandline -t)[-1] case "!" commandline -t $history[1]; commandline -f repaint case "*" commandline -i ! end end function bind_dollar switch (commandline -t)[-1] case "!" commandline -t "" ...


1

This can have several reasons: You might have an alias for tmux which sets special command line options or tmux on some machines does not start a login shell. You could check Why ~/.bash_profile is not getting sourced when opening a terminal? to figure out more. or You might have a code block like the following in one of your bash files: if [[ -n ...


1

You should use escape sequence backslash(\) inside awk command as below. Since you use the command inside double quotes the variables will be resolved before executing. So it will try to understand $4 as a system variable and try to resolve it. If you use escape sequence backslash it will retain the $4 to the awk command. iphost="$(ssh root@$machine -x ...


0

Try this in ~/.bashrc or similar export iphost="$(echo $SSH_CLIENT | awk '{print $1}')"


0

Using an environment variable may be tricky.It might be a matter of the shell you're using: some shells use $HOST, others use $HOSTNAME. I would go with the uname -n option that @grawity mentioned.


3

Use hostname or uname -n to get the kernel hostname (nodename). hostname -s will give just the first component of the same. Use hostname -f to get the FQDN – it additionally tries to translate the hostname to an IP address, then back to a domain name.


2

This will run the command in the background and write errors to a log file whilest ignoring normal output command > /dev/null 2> /tmp/example_error.log &


16

Unfortunately, the assumption that stderr is only used for error output isn't always correct. Rather, stderr is often used for any and all interactive output and diagnostics, i.e. output intended for the user to read in an interactive prompt1. wget and dd are well-known examples. Some commands will provide a flag (e.g. -quiet or -silent) to suppress ...


0

I posted the same problem here on GitHub here and here. And finally solved it, only to find the issue results from a setting :set shellcmdflag=-ic. After I commented that out from .vimrc all aforementioned problems were solved. Think twice before tell vim always to use an interactive shell because it will cause problems, as said here: Telling Vim to ...


12

Unix utilities send general messages to stdout, and error messages to stderr, so if we only want to see error messages, then it will be sufficient to suppress stdout so only stderr gets output to the console. The way to do this (in both bash and fish) is to append >/dev/null to the command. This pipes stdout into nothingness, but stderr (with your error ...


1

By "table", I assume you mean "array". With bash version 4, use the mapfile command: mapfile -t iparr < test.txt In your loop, don't forget the quotes! for ip in "${iparr[@]}"; do echo "$ip" done


0

There are plenty of resources on-line. For example: Unix Shell Scripting Tutorial Linux Shell Scripting Tutorial - A Beginner's handbook The Beginner’s Guide to Shell Scripting: The Basics


1

First identify your shell, as noted in another answer this is simply done via the ps command ... $ ps PID TTY TIME CMD 3360 pts/2 00:00:00 bash 3702 pts/2 00:00:00 ps To be sure which files are being read you will need to investigate the system which you are logging into. 'bash' shell has many 'if this' then 'do that' otherwise 'do ...


0

/etc/passwd last field of /etc/passwd will tell you your loggin shell. ps running ps will tell you also (and level of shell) PID TTY TIME CMD 5802 pts/0 00:00:00 bash 6292 pts/0 00:00:00 ps


0

You could run commands based on a signal trap. Usually a disconnection would end the shell with a SIGPIPE, so add this to the .bash_profile trap "echo do your command here; exit" 13 You need the "exit" otherwise the shell won't close as a result of the trap.


1

By default, read will only read only one line at a time. For example, here we provide three lines of input and, as you can see, read only reads the first line: $ IFS=$'\n' read -ra displays <<< $'a\nb\nc\n'; declare -p displays declare -a displays='([0]="a")' The -d option can be used to change this behavior. For example: $ IFS=$'\n' read -d ...


0

This mailing list thread appears to explain away these problems; quoting from the 2nd msg: The current code in bashhist.c:maybe_append_history() (which has existed for at least 15 years) seems to not handle the case where the number of history lines in the current session is equal to the number of history list entries. That is, the code won't ...


2

You should have << instead of <<<. Or perhaps: ssh -t -t server-a.com ssh -t -t server-b.com '" cd /pylons/web/app/ . envs/bin/activate paster shell /lib/config.ini "'


1

Sorting the output by creation time is impossible in Linux (ctime is not file creation date). stat has the ability to show a file's birth time using the %w and %W format tags, but they always show - and 0, respectively, even on filesystems that store creation time/birth time. Hence, it is practically useless for this purpose on Linux. The other two sorting ...


0

Depends on the way your variable may look in other instances. It could be as simple as: echo $sea | cut -d '-' -f 7- | cut -d . -f -4 or echo $sea | sed 's#.*\(DMJK.*\).tgz#\1#g'


0

How are you determining which characters to extract? If the characters always appear in specific places in the string, e.g. if its a fixed length string or the characters you need are the last 21 characters before a dot and 3 character extension, you could use one of the following in a Bash script: ...


1

How you want to choose what to remove and what to keep is not at all clear. This is one of many ways to get the result that you ask for: $ sea=xyz-ajay-no-siddhart-ubidone-fdh-DMJK.UK.1.0-32133-Z-1.tgz $ sea2=${sea/*[[:lower:]]-/}; echo ${sea2%.*} DMJK.UK.1.0-32133-Z-1 Alternatively, this includes the parts starting with DMJK and leaving off the final ...


2

I would change you script like so: #!/bin/sh for file in /folder/path/* do curl -u username:password -T ${file} http://www.example.com/folder/${file} done Note that the for-loop variable file is used with curl. Better way is to upload using find + curl (as was answered on SO): find /folder/path/ -name '*' -type f -exec curl -u USERNAME:PASSWORD -T ...


0

I used '//apps/matlab2014b$/bin/win64/MATLAB.exe' -r filename and it worked in my shell script. DONOT use .m in the filename above.


2

The answer depends on how you start Cygwin. If you run Cygwin.bat, modify the last line of /Cygwin.bat (usually C:\cygwin\ or C:\cygwin64\). If you use mintty, then add the shell as a parameter. mintty /usr/bin/zsh -


0

This is a hack: put this as the first line of your ~/.bash_profile: exec zsh


1

If the PATH value would be too long after your user's PATH variable has been concatenated onto the environment PATH variable, Windows will silently fail to concatenate the user PATH variable. This can easily happen after new software is installed and adds something to PATH, thereby breaking existing installed software. Windows fail! The best fix is to edit ...


0

If I understood what you want is a function to leave certain number of spaces after a specified string. Here it is: fix_space() { string="$1" len="$2" perl -e "my \$str = '`printf "$string" | sed "s/'/\\'/g"`'; print \$str.' ' x ($len - length(\$str))" } echo "'`fix_space "ololfdsaf" 20`'" echo "'`fix_space "ololff" 20`'" It simply makes perl ...


0

Try this hack. It seems to work in emacs 24.5: (defun win-file-name-completion-advice (res) (if (stringp res) (replace-regexp-in-string "/" "\\\\" res) res)) (advice-add 'comint-completion-file-name-table :filter-return #'win-file-name-completion-advice) (defun win-command-completion-advice () (let ((filename ...


1

You can use pipe output as a shell script argument. Try this method: cat text.txt | xargs -I {} ./Myscript.sh {}


0

From Dot slash explanation The difference you do is, with sh, you're running a program that will interpret the lines in your script just as it you would have typed them on the interactive prompt of the terminal, with ./ you're making a shortcut assuming that the script is just right here in the current directory you're sitting in AND it will be executable ...


0

a child bash process is create same as above (due to shebang) the two are equivalent, if you do not want to fork a child, or want to keep env value from script.sh, use . ./script.sh beware however if script.sh call exit, you will exit current shell. if shell end with no error you are return to initial shell.


1

When bash is invoked as */sh, only /etc/profile and ${HOME}/.profile are read. I have these lines in my .profile (using OS X): if [ -n "${BASH_VERSION}" ]; then # include ~/.bashrc if it exists if [ -f "${HOME}/.bashrc" ]; then . "${HOME}/.bashrc" fi fi My .bashrc contains (among other things) these lines: if [ -f ~/.bash_aliases ]; ...


0

it's usually simpler to just put all aliases and customizations to the shell into ~/.bashrc and then have your ~/.bash_profile source that file in it i.e. source .bashrc but if you make alias changes in your ~/.bashrc they will not take affect until you reload your shell or source you're ~/.bashrc file.


2

This will run all executable files named savetodatabase.sh under the 2014 directory and its subdirectories, and each executable will be run within its directory. #!/usr/bin/env bash find 2014 -type f -executable -name savetodatabase.sh | while read script do cd "$(dirname "$script")" sh "$(basename "$script")" cd - done This assumes, of course, ...


2

Currently you are in fact running the scripts from the directory you have saveall in. If you want to run each of the scripts in their folders (I assume you might have some output that is logging to the pwd?) then the easiest means would likely be to cd into each of the folders and then running the individual sh scripts. So something like: #!/bin/bash cd ...


1

Shell variables only resolve between Double Quotes ("). sed "s/$WORKDIR/$ARCHIVE/ig" test.dat > abc (Double quotes) Would work if not for the forward slashes. Sed can use any character to delimit those input fields and forward slash is perhaps not the best choice due to it's use for directory paths. For example you can use this instead: sed ...



Top 50 recent answers are included