Tag Info

Hot answers tagged

24

There's a general buffering rule followed by the C standard I/O library (stdio) that most unix programs use. If output is going to a terminal, it is flushed at the end of each line; otherwise it is flushed only when the buffer (8K on my Linux/amd64 system; could be different on yours) is full. If all your utilities were following the general rule, you would ...


19

This can be easily done with bash/ksh93/zsh arrays: a=(*) cp -- "${a[@]: -4}" ~/ This works for all non-hidden file names even if they contain spaces, tabs, newlines, or other difficult characters (assuming there are at least 4 non-hidden files in the current directory with bash). How it works a=(*) This creates an array a with all the file names. ...


7

If you are using zsh you can enclose in parenthesis () a list of so called glob qualifiers which select desired files. In your case, that would be cp *(On[1,4]) ~/ Here On sorts file names alphabetically in reverse order and [1,4] takes only first 4 of them. You can make this more robust by selecting only plain files (excluding directories, pipes etc.) ...


5

So long as you find the shell sort agreeable, you can just do: set -- /path/to/source/dir/* [ "$#" -le 4 ] || shift "$(($#-4))" cp "$@" /path/to/target/dir This is very similar to the bash-specific array solution offered, but should be portable to any POSIX-compatible shell. Some notes about both methods: It is important that you lead your cp arguments ...


5

This actually took me some thought to understand and even more to answer. Great question (I'll upvote it next). You neglected to try tr | sed in your debugging items above: >tr '[:lower:]' '[:upper:]' | sed 'p' i am writing still writing now ctrl-d I AM WRITING I AM WRITING STILL WRITING STILL WRITING NOW CTRL-D NOW CTRL-D > So evidently tr ...


4

The fc built-in command allows you to extract commands from the history using a number of criteria (see man zshbuiltins for details). fc stands for “fix command”, and when invoked with no parameters it opens an editor with the last command entered. You can use all your editor’s features to change the command, and when you save and exit zsh runs the fixed ...


4

Should I output to a temporary file, then copy it over the final file only if the backup works? Not copy but rename. But this is impossible for the backup script if it writes to stdout. It cannot even prevent the empty file from being created as that has already happened when the script starts. This must be done from the outside. if ./script.sh ...


3

I think your executable file just prints a to the screen. When you write $COMMAND, file executed and the output tried to be executed again.Because it cannot execute a command, it gives an error. You can use just simply $COMMAND or echo `$COMMAND`


3

Here is a solution using extremely simple bash commands. find . -maxdepth 1 -type f | sort | tail -n 4 | while read -r file; do cp "$file" ~/; done Explanation: find . -maxdepth 1 -type f Finds all files in current directory. sort Sorts alphabetically. tail -n 4 Only show last 4 lines. while read -r file; do cp "$file" ~/; done Loops over each ...


3

If there are only files and their names do not contain whitespace or newline (and you've not modified $IFS) or glob characters (or some non-printable characters with some implementations of ls), and don't start with ., then you can do this: cp -- $(ls | tail -n 4) ~/


3

First, move the existing executable to a new location: mv ./executable ./executable.original Second, create a shell script in it place. Place the following commands in a new file called ./executable: #!/bin/sh LD_PRELOAD=/some/lib ./executable.original my_arg1 my_argn "$@" some_other_command The shell will replace the incantation "$@" with arg1 arg2 ...


3

Looking specifically at your cmp command, replace: if [ cmp ${TMPFILE} ${GDIR}/comparel ]; then With: if cmp "${TMPFILE}" "${GDIR}/comparel"; then One uses the [ command (also known as test) when one wants to set an exit code according to one of various conditions (a file exists, a string is empty, etc.). Here, you want to set an exit code based on ...


2

If you are using the console ttys (/dev/tty1 through /dev/tty7) in text mode you can read the screen buffer directly from the corresponding /dev/vcsN device. You have to know that there are 80 characters per line, so simple maths to convert an (x,y) coordinate to offset (y*80 + x) will get you the desired character. #!/bin/bash # Ranges are 1-80, 1-24 # ...


2

The code in question doesn't actually depend on /dev/tcp existing on a filesystem. Rather, it is a feature of Bash itself. For example, if you write a simple HTTP request to file descriptor 3 in your example, you should get a response: > ls /dev/tcp ls: /dev/tcp: No such file or directory > exec 3<>/dev/tcp/google.com/80 > echo -e "GET / ...


2

I'm afraid there's no portable solution especially if you want to consider variables with no value part or duplicate variables. On a recent GNU system: env -0 | grep -z '^T' | tr '\0' '\n'


2

A simple loop will do the trick. cd working for dir in */*/; do [ -e "$dir/files.zip" ] || # skip directories where the zip already exists ( cd -- "$dir" && zip -r files.zip .) done Note that zip is smart enough to skip the zip file that is being built when recursing in that directory. Some other archiving programs would attempt to stuff the ...


2

There is nothing wrong with that syntax if the shell supports arrays. Most likely the script starts with #!/bin/sh and wrongly uses specific shell features such as bash. If the shebang is #!/bin/sh, change it to #!/bin/bash and report the problem to the CINT developers.


2

I don't understand why you don't want to use .. Anyway you can use some character class instead where . is included, but other chars from your directory not, good candidate for this purpose can be [[:punct:]]: LC_ALL=C ls -- *[[:punct:]]txt I changed locale to C as character classes depend on that, and added -- to ls option in order to list all files ...


2

With zsh: vi ./**/*_test.mov(.s:_test.mov:_info.txt:)


2

When inotifywait outputs the string "Watches established." is secure to make changes in the watched inodes, so you should wait to the string to appears on standard error before touching the files. As an example,this code should to that, inotifywait -r -m -e close_write "somedir" \ 2> >(while :;do read f; [ "$f" == "Watches established." ] && ...


2

Use [!x]txt to list all files ending in txt but exclude the ones ending in xtxt: ls *[!x]txt or use ASCII code for dot, e.g.: ls *$'\x2E'txt or ls *$'\056'txt


2

If you are using the Bash shell: shopt -s extglob ls !(*xtxt|*bak|*bat)


1

Extremely ugly, since this could be achieved using find . -type f -name "*.txt" easily, but if you don't want to put an . in there, and the only file extensions you are going to find in that directory are .bat, .bak, .txt, and .xtxt, then you can try this: ls *txt | grep -v "xtxt$" ls *txt will bring anything ending with txt, and grep -v "xtxt$" will ...


1

Assuming your are on a Linux system, or at least that you have GNU touch and GNU date, you can do (in bash): $ shopt globstar $ for f in **; do touch -d "$(date -d "$(stat -c '%y' "$f") +3 months")" "$f" done That, however, will ignore hidden files. To match those as well, run shopt -s dotglob before the above commands. Explanation shopt -s ...


1

From the description of the problem I believe you are looking for the tool which can get time of last file modification and then add 3 months to it. You can do it with stat + touch + some shell arithmetic evaluation. For example to add 90 days to file timestamp you can write touch -d "@$(( $(stat -c '%Y' file) + 90*24*3600 ))" file Then just loop over all ...


1

You can use cut to remove unwanted columns. From the man page: -d, --delimiter=DELIM use DELIM instead of TAB for field delimiter -f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified --complement complement the set of ...


1

1. get a acount which is a sudoer su - echo 'YOU ALL=(ALL:ALL) ALL'>/etc/sudoers exit 2. disable the root account sudo usermod --expiredate 1 root 3. test it $ su - Password: <correct password> Your account is expired. Tested on Debian GNU/Linux


1

From wget manual: ‘--user=user’ ‘--password=password’ Specify the username user and password password for both FTP and HTTP file retrieval. These parameters can be overridden using the ‘--ftp-user’ and ‘--ftp-password’ options for FTP connections and the ‘--http-user’ and ‘--http-password’ options for HTTP connections. So what you ...


1

With awk, it's awk -F '|' -v OFS='|' '{sub(/^.../, "& ", $4); print}' file But that cannot edit in-place, so you have to: t=$(mktemp) awk -F '|' -v OFS='|' '{sub(/^.../, "& ", $4); print}' file > "$t" && mv "$t" file With sed, sed -i 's/^[^|]*|[^|]*|[^|]*|.../& /' file If you want to validate the postal code, then sed -i ...


1

You don't need to use any >> Just type env | grep ^T.



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