New answers tagged

2

You could: To check for range you could use an if statement or a continue operation: if [[ $z -ge $1 || $z -le $2 ]] then : print result fi or [[ $z -lt $1 || $z -gt $2 ]] && continue : print result Depending on needs, the test options are: -ge: Greater than or equal to -le: Less than or equal to -lt: Less than -gt: Greater than Use ...


2

for i in * do size=$(stat --format="%s" "$i") if [ $size -gt $1 -a $size -lt $2 ] then printf "Right-sized: %s\n" "$i" else printf "Wrong-sized: %s\n" "$i" fi done This is just an update to your loop, but instead of parsing ls, it uses stat to gather the file's size, in bytes. This snippet assumes that you've already sanity-checked $1 ...


1

What you need is a condition, which in shell has the following syntax: if TEST then ACTION else ACTION fi For tests, it's common to employ [. For example, to check if $5 is greater than 25 but is less than 50, you'll write: if [ "$5" -gt 25 ] && [ "$5" -lt 50 ] then echo "It's greater than 25 but is less than 50" else echo "It's ...


6

Bear in mind that dd is a raw interface to the read(), write() and lseek() system call. You can only use it reliably to extract chunks of data off regular files, block devices and some character devices (like /dev/urandom), that is files for which read(buf, size) is guaranteed to return size as long as the end of the file is not reached. For pipes, sockets ...


0

The rsync way given by Thane with Yamaneko additions work great but leave empty directories. For me the final solution was in two steps, first call rsync with full path, then a find command to remove all empty directories: rsync -rvcm --compare-dest=/tmp/org/ /tmp/new/ /tmp/difference/ find /tmp/difference/ -d -type d -empty -exec rmdir {} \; -print ...


2

With a bashism, and a functionally "useless use of cat", but closest to the syntax the OP uses: cat <(dd if=file1 bs=1M count=99 skip=1) \ <(dd if=file2 bs=1M count=10) \ > final_output (That being said, Stephen Kitt's answer seems to be the most efficient possible method.)


12

dd can write to stdout too. ( dd if=file1 bs=1M count=99 skip=1 dd if=file2 bs=1M count=10 ) > final_output


8

I don't think you can easily read multiple files in a single dd invocation, but you can append to build the output file in several steps: dd if=file1 bs=1M count=99 skip=1 of=final_output dd if=file2 bs=1M count=10 of=final_output oflag=append conv=notrunc You need to specify both conv=notrunc and oflag=append. The first avoids truncating the output, the ...


1

My El Capitan has no setattr or setxattr (the latter is the name of a C runtime function: you could make your own utility using that). OSX provides xattr, which (noting comments such as Mac OS X Extended Attributes and Xattr) seems fairly recent. A comment in Manually set extended attributes on arbitrary files from 2011 gives a hint about OSX 10.5 You can ...


0

It's called xattr on OS X . You can use setxattr to set attributes.


0

By itself, lsof -i will only list Internet information. You can add file information (or show that instead) using the -d option. For example: lsof -d txt | grep -E '/httpd.pl$' because (unless a process changes the name under which it runs) the command name will match the actual filename which was loaded. That file has to be executable, of course ...


2

You can use the commande file e,g: file images.jpg the output is something like : images.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 342x147, frames 3 OR rdjpgcom -verbose images.jpg sample output JPEG image is 342w * 147h, 3 color components, 8 bits per sample JPEG process: ...


4

I can only address the first part of your question: You can view the dimensions of an image on the command line by the using the identify tool, part of the the imagemagick package. (To install imagemagick on a Debian box, provided you have sudo privileges, you can run sudo apt-get install imagemagick ). For example, in a directory with image file rose.jpg, ...


2

There are number of tools that will do this: identify from ImageMagick jhead jpeginfo some versions of the file command If these programs are not installed, note that both jhead and jpeginfo are quite simple and presuming a compiler is available will be easy to build in your own user account.


1

Most but not all files that are part of the system are owned by the root user. It's rare for system files not to be owned by root, because a user that owns system files can modify them and this is usually not desirable. It's a lot more common to have files that are owned by a group other than root, and that have mode 660 or 664 or 640. It's possible to ...


1

You can use find to find all of the files in a directory structure that you want to run through your dos2unix command find /path/to/the/files -type f -exec dos2unix {} \; Take a look at the man pages for find, there are a lot of options that you can use to specify what gets evaluated


2

find /path -type f -print0 | xargs -0 dos2unix --


0

Use a wildcard. Like this: (If you're in the folder) dos2unix * or if you're outside of the folder do: dos2unix /path/to/folder/*


1

If you have a ext2/3/4 filesystem you can use debugfs for a low-level look at an inode. For example, to play without being root: $ truncate -s 1M myfile $ mkfs.ext2 -F myfile $ debugfs -w myfile debugfs: stat <2> Inode: 2 Type: directory Mode: 0755 Flags: 0x0 Generation: 0 Version: 0x00000000 User: 0 Group: 0 Size: ...


-1

an inode wil store only one file. try find /xxx -xdev -inum 1234 -print where /xxx is mounting point -inum 1234 search for an inode number 1234 -print self explainatory This suppose /xxx is mounted an healthy.


1

The answer is no. All files outside of home directories are not owned by root. There is a rich and complex set of users and groups, and permissions, on files outside of user home directories for a number of reasons, some historical, and some security related. However, tar implementations have two usual modes, they either, restore files and directories ...


0

If you have the util-linux package installed, you set hard and soft limits for a particular process using the prlimit command. To set the soft and hard limits for maximum number of open files to 10 and 20, respectively, for a command command, run: prlimit -n10:20 command Arguments for the command can be provided as well: prlimit -n10:20 command arg1 ...


0

A shell-only alternative: process batches of COUNT files via "${@:START:COUNT}" (range of positional parameters) and shift COUNT while incrementing a counter c to name the archives: set -- * c=1 while (($#)); do if [ $# -ge COUNT ]; then zip ${c}.zip "${@:1:COUNT}" c=$((c+1)) shift COUNT else zip ${c}.zip "${@}" shift $# fi done


3

Not sure what you are doing wrong but the following works: for i in *; do cp "$i" $(date '+%Y%m%d')"$i"; done (you should only run this once in a directory)


2

Came looking for a one-liner on MacOS. Settled on the following. Compiled and added this tool to my path. This took less then 30 seconds. $ git clone git@github.com:sschober/kqwait.git $ cd kqwait $ make $ mv kqwait ~/bin $ chmod +x ~/bin/kqwait Next, I went to the directory in which I wished to do the watching. In this case, I wished to watch a markdown ...


5

The history is in ~/.local/share/recently-used.xbel but it is not sufficient to remove that file. If you do the Recent Files entry keeps on showing the files you accessed, and if you open a file with an application that creates an Recent Files entry, that and the old list of files will be written to that file again. What you want to do is make an alias or ...


0

Other option could be: echo -n > filename From the man page of echo: -n Do not print the trailing newline character.


5

Do not add yourself to the root group, this many have many unintended side effects granting more than you intended. These directories are intentionally not writable by normal users. In the event you need to make manual changes to them (which will be rare), you can perform those operations as root via sudo.


1

awk can write to different files by itself. e.g. awk '{print > "new/"FILENAME}' *.txt You could use something like that inside your NormalizeFiles.awk script. If you don't want to type > "new/"FILENAME for every print statement, you could do something like this: awk 'FNR == 1 { out = "new/"FILENAME } ; { print > out }' *.txt That changes the ...


1

It appears you are trying to run input files through awk and save the results as a different file name. Unless you need very special file naming, this should serve: for i in *.txt; do awk -f NormalizeFiles.awk $i > $i.out done Although your question is tagged bash, your syntax, as someone pointed out, is not bash, e.g. the misuse of parentheses. ...


0

If you want to do it with sudo instead of su, you could do this: sudo sh This will log you in as root on the terminal, then you can do what you want to do. But I wouldn't advise you mess with /root, because there must be a reason it is not assessable by other users. You should move '1.txt' somewhere else. Hope this helps!


6

Example using bash string manipulation only to extract the date: #!/bin/bash for name in IMG-[0-9]*.jpg; do touch -amt ${name:4:8}0900 "$name" done


1

From your example, assuming that all of the files have a valid yyyymmdd date, you can extract the date from the filename and apply that in the command cited: #!/bin/bash for name in IMG-*-W*.jpg do date="$(echo "$name" | sed -e 's/^IMG-//' -e 's/-W.*//')" touch -a -m -t ${date}0900 "$name" done If some file hasn't a valid date, that is more work. ...


4

There are many possible solutions for that: You can configure sudo not to require tty: RequireTTY in /etc/sudoers You can force tty allocation on command-line in these specific cases, where you need it: ssh -tt host command You can tell scp not to allocate TTY by -T or -o RequestTTY=no command-line option: scp -T file host:path/ or scp -o RequestTTY=no ...


0

There are a lot of good answers here that helped me get going but I ended up with this simple command: zgrep -e 'something to search for' *.gz


5

SCP protocol is binary one. With TTY enabled, the control characters have their meaning. So as soon as the TTY sees a character in the SCP protocol binary data that appears as a control character, it interprets it. Particularly as soon as there's ^C (ASCII 0x03), it aborts the SCP process. Use ssh -t to force TTY for interactive sessions, instead of ...


1

It is apparent that the /root directory on your system does not have (at least) 'execute' permisions for the user 'yu', either through group membership or the 'other' bits. Thus, 'yu' cannot list the contents of the /root directory in order to determine if /root/1.txt exists or not. For further description of file and directory permissions (in Linux, as ...


3

You can do this with find: cd top_level_dir find . -type f -exec bash -c "echo -n '' > {}" \; For every filename it invokes Bash (not very efficient) and echos, without newline because of the -n, essentially nothing to the file, thereby overwriting its contents and creating a zero length file. If you have a lot of files and/or have to this often a ...


2

For organising your movie collection and automatically adding XBMC-compatible XML metadata files, I'd highly recommend CouchPotato. Then, depending on what you want out of your viewing experience, I'd recommend either XBMC for customisability, or its closed-source derivative, Plex, for appearance and ease of use by less technical viewers. For music, check ...


3

In case you want to understand the original command, let's go though that step by step. find -not -empty -type f Find all non-empty files in the current directory or any of its subdirectories. -printf "%s\n" Print its size. If you drop these arguments, it will print paths instead, breaking subsequent steps. | sort -rn Sort numerically (-n), in ...


6

You can make it shorter: find . ! -empty -type f -exec md5sum {} + | sort | uniq -w32 -dD Do md5sum of found files on the -exec action of find and then sort and do uniq to get the files having same the md5sum separated by newline.


10

You can use fdupes. From man fdupes: Searches the given path for duplicate files. Such files are found by comparing file sizes and MD5 signatures, followed by a byte-by-byte comparison. You can call it like fdupes -r /path/to/dup/directory and it will print out a list of dupes. Update You can give it try to fslint also. After setting up fslint, go to ...


6

Don't. /root/ is not world readable for very good reasons. If you really need to work with that file, put it someplace else.


2

An excerpt from man sysctl.conf: Comments are denoted by a "#" at the beginning of a line.


0

You can use Vim in Ex mode: ex -sc '1i|ALFA' -c '$a|BRAVO' -cx file 1 select first line i insert text and newline $ select last line a append text and newline x save and close


1

You can use Vim in Ex mode: replace string ALF with BRA in all files in the current directory? for CHA in * do ex -sc '%s/ALF/BRA/g' -cx "$CHA" done do the same recursively for sub directories? find -type f -exec ex -sc '%s/ALF/BRA/g' -cx {} ';' replace only if the file name matches another string? for CHA in *.txt do ex -sc ...


3

The command you are looking for is touch with the option -d (or --date) touch - change file timestamp (...) -d, --date=STRING parse STRING and use it instead of current time For example: touch -d '2016-01-15 22:41:18' file or even touch -d '3 months ago' file As always man touch is your friend.


5

My understanding of the question is that, you need to create a large file, each line of this file is a random word within specified length. If you don't need the word to be a real word, but some random characters: < /dev/urandom tr -d -c '[:alpha:]'|head -c 1M|fold -w10 >result.txt This will create a file of size 1M and each line with 10 random ...


1

Don't call rm -rf. As soon as you run it on a directory, that deletes all the files in the directory and the subdirectories, even the ones you wanted to keep. More generally, don't call rm -rf unless you know why a simple rm won't do. Here, rm -f is warranted if you want to be sure that your script won't ask for confirmation to delete a read-only file, but ...


4

awk 'NR==1 {a=$0} (NR-1)%100==0 {print a > "d/file_" int(1+(NR-1)/100)} {print > "d/file_" int(1+(NR-1)/100)}'



Top 50 recent answers are included