Tag Info

Hot answers tagged

11

Your confusion stems from the fact that in many popular languages, especially C-based ones, && sequences are stopped being evaluated when 0 is encountered, because only 0 is considered false and everything else is true. In Bash, however, it's not the case. By convention, in POSIX systems (and all other Unix-like systems), return code 0 is considered ...


10

Use tar: tar -cf my_big_folder.tar /my/big/folder Restore the archive with tar -xf my_big_folder.tar -C / -C will change to the root directory to restore your archive since the archive created above contains absolute paths. EDIT: Due to the relatively big size of the archive, it'd be best to send it [directly] to its final location, using SSH or a mount ...


9

That is a feature of the shell that remembers how you got to where you are. If you have realpath installed you can do: $ realpath /home/dazz/test/1 And lacking that if you have python: $ python -c "import os; print(os.path.realpath('.'))" /home/dazz/test/1 or readlink (from coreutils): $ readlink -f . /home/dazz/test/1 or /bin/pwd (not the shell ...


8

The return value from commands are different from typical boolan values. 0 is success when executing a command, anything else is failure. && expects 0 to me success here for that reason.


6

The answer of Michael is not correct. If the name has a space you are in trouble: $ ls aaa bbb ccc ccc a the last item is "ccc a" $ ls | grep -o -w '\w\{3\}' aaa bbb ccc ccc Celada said the correct answer: $ ls ??? aaa bbb ccc $ ls ????? ccc a


6

You're leaving a zombie, trivially, because you didn't wait on your child process. Your shell is immediately exiting because you've set up its STDIN in a nonsensical way. pipe returns a one-way communications channel. You write to pipefd[1] and read it back from pipefd[0]. You did a buch of dup2 calls which lead the shell to attempt to read (STDIN) from the ...


6

Other answers have covered /bin/pwd vs the shell's builtin pwd. If you want to follow symlinks in the Windows style you mentioned, use cd -P: it will change the PWD variable accordingly. If you want to use -P by default, you can add this line to your .bashrc or .zshrc: set -P Other shells may vary.


5

Contrast pwd and /bin/pwd. pwd, which is a built-in command in many shells, tells you where your shell thinks you are (and hence treats symlinks "soft links" as if they were real directories. /bin/pwd is an external program that tells you where you really are, if necessary by traversing the filesystem tree up to /. It takes no account of symlinks because ...


4

In most terminal emulators, long command lines […] are wrapped to a new line before the user submits the command by pressing Enter. This is not a function of the terminal emulator. It is a function of your shell. Your shell is not a full-screen application, but it is doing cursor addressing. When you are editing a command line in a shell, ...


4

find is an executable not an awk function. S, if you want to call an executable within awk, you have to do that with the system() function. cmd | awk '{system("find " $6 " -xdev -type f -perm -4000 -print")}'


4

The same tools that you can use for other files (generally) can also be used on block devices. This means that you can use, for example, xxd or hexdump to inspect the filesystem: $ sudo xxd /dev/sda2 | head -10 00000000: eb58 9053 5953 4c49 4e55 5800 0200 0000 .X.SYSLINUX..... 00000010: 0000 0000 0000 0000 3f00 ff00 0008 2000 ........?..... . 00000020: ...


3

In most cases, that would be the redirection operator (<): $ tr 'a' 'b' /path/to/file ## fails because `tr` works on streams tr: extra operand ‘file’ Try 'tr --help' for more information. $ tr 'a' 'b' < /path/to/file ## works because the file's contents are passed to tr Both command substitution and the redirection operator are defined by POSIX ...


3

There is no way to achieve this within a script - scripts start subshell, which is a standalone environment. There's all sorts of reasons for this, but pretty fundamentally - a script cannot tamper with your environment (including your cwd). The closest you get is creating an alias within the current shell. alias chr="cd /" Either that, or 'source' the ...


3

Here, also using zsh, I have echo hai && echo bye hai bye And similarly echo hai && echo %? hai 0 Are you sure that you are seeing hai and bye on the same line with exactly the commands you have provided here? In direct answer to your question, an exit status of zero is success, so the second statement is executed. (This allows ...


3

From man expr ARG1 % ARG2 arithmetic remainder of ARG1 divided by ARG2 4 % 4 evaluates to 0 as that is the remainder ARG1 / ARG2 arithmetic quotient of ARG1 divided by ARG2 4 / 4 evaluates to 1 as that is the ratio of these two numbers


2

the which command you're using assumes the argument "my_command" is inside your PATH. which searches via your global environment variable PATH for the argument that you type and lists the complete path to the first match it finds. You can find your path by typing env on the command line and looking for PATH=. whereis - searches for files in a restricted ...


2

If you are using bash, you can set the nullglob option: nullglob If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves. To illustrate: $ ls test1.dummy test1.jpeg test1.jpg test1.test $ shopt nullglob nullglob off $ for file in ...


2

Here is an awk way to achieve what you want: awk ' NR==FNR { k[$1] ; next } { for (i=1; i<=NF; i++) if($i in k) $i="" ; gsub(/ +/," ") } 1 ' champs.txt t.txt (The gsub is just for convenience, it compresses sequences of blanks.) For you sample data the result is: * student name age professors departement DPTNUM= 20


2

The command cp -f * /var/www/ copies files matching * in the caller's current directory, i.e. your current directory. It is irrelevant where the script is located.


2

Based on this answer I believe using varname="ENV_$env" echo ${!varname} could be a solution.


2

Well, you absolutely can using eval as follows: c=0 for file in $( ls ); do eval "var$c=$file"; c=$((c+1)); done This code will create variables named var0, var1, var2, ... with each one holding the file name. I assume you will have a good reason you want to do that over using an array ...


2

expr shall evaluate an expression and write the result to standard output. % is modulo operator, / perform division operator. 4 % 4 evaluated to 0 4 / 4 evaluated to 1


2

In bash (and some other shells, like zsh), you can use $_, which contains the last argument to the previous command: mkdir /path/to/file cd "$_"


1

Alternative: cpio (cd /my/big/folder && find . -depth -print0 | cpio -0o > myfolder.cpio) Unpacking to current directory: cpio -id < myfolder.cpio Caveats: If use find /my/big/folder instead of cd, the archive will contain full paths and extraction will try to follow them; Big files (> 2GB) may be a problem;


1

Possible you mean array. There are some ways to assign values First: c=0 for file in $( ls ); do var[$c]="$file"; c=$(($c+1)); done Second: c=0 for file in $( ls ); do var[c++]="$file"; done Third: for file in $( ls ); do var[${#var}]="$file"; done Fourth: var=( $(ls) ) Fifth ... there is no need to use ls , just put * for ...


1

GNU date accepts a number of relative dates that you can supply with the -d flag. $ date -d '+1year' Sun Apr 17 09:15:14 PDT 2016 See Relative items in date strings for details.


1

If you want to let date create the standard date formatting you could do: $ date -d "$( date -d "Fri Apr 17 20:16:01 IST 2015" "+2016-%m-%d %T" )" But you can also use just one date instance and use the appropriate time format specifiers, as in: date -d "Fri Apr 17 20:16:01 IST 2015" "+%a %b %e %T %Z 2016" Or - since your date format has the year at ...


1

You need to escape the quotes. Think of them as layers of wrapping, in a game of pass the parcel. Each shell unwraps a 'layer'. So: echo "this ; is a semicolon" But if you wanted to run that via ssh: ssh $user@$host echo "this ; is a semicolon" The ssh would unwrap the first layer of the package - sending: echo this ; is a semicolon Which would ...


1

You need to catch the SIGCHLD signal, and "reap" the zombie process with a wait() system call. Nearly minimal addition of code to your program to add a signal handler function, and set it as the SIGCHLD handler looks like this: #include <cstdio> #include <fcntl.h> #include <cstring> #include <stdlib.h> #include <cerrno> ...


1

In principle your solution should work, but it is not robust. If, for example, you have in the greped file a comment (or something else) in the matched line, say, like: SOME_PATH=/some/path # this is some path then the comment will also be part of the name that you expand in path. To check that inspect what's actually in the path, e.g. by printf "'%s'\n" ...



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