Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Hot answers tagged

4

Only the search expression is a regex, the replacement text isn’t: sed -i -e "s/'}$/\"}/g" This matches '} at the end of a line, and replaces it with "}.


4

Although there's a small race condition you could use something like this: src=from.txt dst=to.txt [[ -f "$dst" ]] && cp -fp "$src" "$dst" The [[ ... ]] operations checks to to see if the file already exists, and if it does the && allows the copy to proceed. If you have rsync you can do it directly (the --existing flag tells rsync only to ...


3

Use stat tool to get inode number. rename directly using mv. Quote filenames (!!), e.g. "$filemove", never $filemove. Add some checking for security before moving [ ! -e "$target" ] && mv ... Use set -euo pipefail at the beginning of the script, so it fails on any error. Use for f in "$@"; do ... done loop to allow multiple files as arguments. Again: ...


3

You can do this in Emacs, which is available as a package on most Unix systems, if not preinstalled. The built-in hex viewer doesn't support deletion, but the improved hex viewer nhexl-mode does. Open the file in Emacs and type Alt+x nhexl-mode Enter. If this says “no match”, type Ctrl+g and install nhexl-mode then try again. To install nhexl-mode, the ...


2

You will not be able to recover the file unless you have a backup to restore it from. For the future, I would recommend a sequence of operations like this vi ~/.crontab # Edit a local file crontab ~/.crontab # Set this file as your crontab file If you get into the habit of only ever editing the copy and not the installed version you ...


1

Your output fragment is incomplete. In your line staff 1344 May 18 11:03 (filename).docx the staff is the group of the file and 1344 is the size. A complete line should look similar to this: -rw-r--r-- 1 user staff 1344 May 18 11:03 (filename).docx Here user is the user name, staff is the group name and 1344 is the size.


1

When sharing filesystems, the system that actually has the physical storage for the shared filesystem is normally called the server, and any system using the shared storage remotely will be a client. So, in your case, your "client machines" will all need to be NFS or CIFS servers too, and your server system will also be a NFS client. NFS is equally ...


1

Use read to ask for confirmation, e.g. like this: echo "fd $f file found..." read -p "Press enter to continue or Ctrl+c to cancel" cp -v "$f" "$1" or echo "fd $f file found..." read -p "Do you want to recover this file? [Yes/No] " confirmation [[ $confirmation =~ ^[YyJj] ]] || { echo "Canceled"; exit } cp -v "$f" "$1"


1

Your command errors out: sed: -e expression #1, char 10: unterminated 's' command so possibly because there is no standard output generated. Try sed -e 's/ \+/ /g' myfile.txt > myfile2.txt Also: https://stackoverflow.com/questions/22939323/sed-command-to-replace-multiple-spaces-into-single-spaces


1

no forks: ls | perl -lne '$suf=substr($_,6); rename $_, "XXXXX-$suf"' When you use a shell loop, the mv forks once per file. Perl's rename command does not. (Perl's rename command has some restrictions, but in this specific case those restrictions don't apply.) As for the rename command shown earlier, yes that works, but then you have all that confusion ...


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