Change file content linux

Changing a file’s «Date Created» and «Last Modified» attributes to another file’s

I’m using merge cap to create a merge pcap file from 15 files. For the merged file, I have changed the name to that of the first of the 15 files. But I would also like to change the merged file’s attributes like «Date Created» and «Last Modified» to that of the first one. Is there anyway to do this?

I try to access the merged files over a samba server (Ubuntu). So that an extractor function can access auto extract the files to D folder. But as the created date will be changed for the merged file the extraction fails. Is there anyway to fix this?

3 Answers 3

You can use the touch command along with the -r switch to apply another file’s attributes to a file.

NOTE: There is no such thing as creation date in Unix, there are only access, modify, and change. See this U&L Q&A titled: get age of given file for further details.

Example

For example purposes here’s a goldenfile that was created with some arbitrary timestamp.

Now I make some new file:

Now apply goldenfile ‘s attributes to newfile .

Now newfile has the same attributes.

Modify via Samba

I just confirmed that I’m able to do this using my Fedora 19 laptop which includes version 1.16.3-2 connected to a Thecus N12000 NAS (uses a modified version of CentOS 5.x).

I was able to touch a file as I mentioned above and it worked as I described. Your issue is likely a problem with the either the mounting options being used, which may be omitting the tracking of certain time attributes, or perhaps it’s related to one of these bugs:

Читайте также:  Астра линукс конференция 2021

Источник

Output file contents while they change

I want to output a file’s contents while they change, for example if I have the file foobar and I do:

The current terminal should display the file’s contents and wait until, I don’t know, I press ^C.

Then if from another terminal I do:

echo asdf >> foobar

The first terminal should display the newly added line in addition to the original file contents (of course, given that I didn’t press ^C).

5 Answers 5

You can use tail command with -f :

It’s good solution for real time show.

If you want to show a short file, that fits on one terminal screen, and what is changing is possibly the whole file, you could use watch :

watch cat example.txt

It shows the whole file every 2 seconds by default, including an optional header:

The option -d ( —differences ) will highlight changes from previous version of the output, or from the first version.

less has a follow mode similar to tail -f — just hit F when you have it open.

When I need to detect file changes and do something other than what tail -f filename does, I’ve used inotifywait in a script to detect the change and act upon it. An example of use is shown below. See man inotifywait for other event names and switches. You may need to install the inotify-tools package, for example via sudo apt-get install inotify-tools .

Here’s the example script, called exec-on-change :

In two consoles I entered commands as follows (where A> means entry in console A, and B> means entry in console B.)

The following output from cat t appeared in console A:

The following output from exec-on-change appeared in console B:

The exec-on-change script terminated when I rm ‘d t .

Источник

How to edit a text file in my terminal

I’m using Linux mint and using the vi command to create text files, now that I created a text file and saved it. How do I get back into to edit the text file again?

6 Answers 6

Try this command:

it, will open up a text editor to edit your file.

OR

Here, you can edit your file in the terminal window.

Читайте также:  Regmon для windows 10

Open the file again using vi. and then press the insert button to begin editing it.

Open the file again using vi. and then press » i » or press insert key ,

For save and quit

and write the following command

without save and quit

If you are still inside the vi editor, you might be in a different mode from the one you want. Hit ESC a couple of times (until it rings or flashes) and then «i» to enter INSERT mode or «a» to enter APPEND mode (they are the same, just start before or after current character).

If you are back at the command prompt, make sure you can locate the file, then navigate to that directory and perform the mentioned «vi helloWorld.txt». Once you are in the editor, you’ll need to check the vi reference to know how to perform the editions you want (you may want to google «vi reference» or «vi cheat sheet»).

Once the edition is done, hit ESC again, then type :wq to save your work or :q! to quit without saving.

For quick reference, here you have a text-based cheat sheet.

Источник

Change content of a file from the terminal

How can I change the content of a file from the terminal? I do not want to use any application like a text editor, since I want to use it for a script.

To give an example, I want to turn the file /etc/apache2/mods-enabled/dir.conf from this

2 Answers 2

or if it’s really static, have an extra copy of the file and cp it.

There are many ways to do this, depending whether you want to rearrange fields on that second line or just set it to exactly what you want. Here is the simplest way to do it:

This will change the DirectoryIndex line, including everything after that word, to DirectoryIndex index.php . The -i.bak saves a backup copy of the original file in dir.conf.bak . After running this, we have:

Not the answer you’re looking for? Browse other questions tagged command-line or ask your own question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Читайте также:  Vmware windows cluster disk

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to change the content of file in linux?

I have 3 configuration files in my Linux box which contains config information for a custom application. I want to change some values in configuration files. The contents of the files and descriptions are given below:

config1 file content:

config2 file content:

config3 file content:

I want change all values 10x_64, 11x, 11x_64 to 12x_64 in all the files. Currently I’m using three commands to change the content; commands are given below:

I want a single generalized command to change the content of all 3 files.

4 Answers 4

If you want a single expression, you can do:

I’ve used /client/[^/]* as the marker to find what we want to replace (ie whatever is after /client/ but before the next / ) , but we could have done client/[^/]*/instance instead if that avoids matching other items in the file.

You can just simply combine them like this:

sed -i ‘s/10x_64/12x_64/g;s/11x/12x_64/g;s/11x_64/12x_64/g’ config*

How about preparing a «template configuration file», in which you replace the needed configuration parts for a particular version? Let’s say you have the following template

Then you could do replace-commands by version depending on a commandline-parameter:

This may be easily extended.

You can combine all three commands into a single expression. What you want to do is replace all cases of 1 followed by either a 0 or a 1 , then an x and then an optional _64 . This can be expressed by the following extended regular expression:

The [ ] denote a character class, so [01] matches either a one or a zero. The (foo)* construct will match 0 or more instances of foo . So, (_64)* will match both the string _64 (and its repetitions like _64_64_64 ) and the empty string. This lets you match both 11_x64 and 11x and when it is not.

So, if we combine that into a sed expression, we get:

Источник

Оцените статью