How to copy and paste a file?
I want copy and paste a file. The name of the file is mkoctfile.m .
The path of this file is: /usr/share/octave/3.2.4/m/miscellaneous/mkoctfile.m I want to paste it to the following path /usr/bin/mkoctfile-3.2.4 I have made the directory by using following commands: sudo su mkdir -p /usr/bin/mkoctfile-3.2.4 but I don’t know how to copy and paste mkoctfile.m in this path. Please tell me what command I have to use.
4 Answers 4
Use the cp command to copy a file, the syntax goes cp sourcefile destinationfile . Use the mv command to move the file, basically cut and paste it somewhere else.
The exact syntax you would use for your example is:
sudo cp /usr/bin/octave/3.2.4/m/miscellaneous/mkoctfile.m /usr/bin/mkoctfile-3.2.4
For more information on the cp or mv commands you can run:
To @BKSurgeon, I would suggest to use the tab key to see the paths/directories available, or type ls to see them all at once printed.
I think it is better to use cp -a than just cp , if you want to have the same affect as when copy-pasting in desktop GUI.
You can cut, copy, and paste in CLI intuitively like the way you usually did in the GUI, like so:
- cd to the folder containing files you want to copy or cut.
- copy file1 file2 folder1 folder2 or cut file1 folder1
- close the current terminal.
- open another terminal.
- cd to the folder where you want to paste them.
- paste
To be able to do so, make sure you have installed xclip and realpath . Then, append these functions to the end of your ~/.bashrc file:
copy() < # if the number of arguments equals 0 if [ $# -eq 0 ] then # if there are no arguments, save the folder you are currently in to the clipboard pwd | xclip else # save the number of argument/path to `~/.numToCopy` file. echo $# >~/.numToCopy # save all paths to clipboard # https://stackoverflow.com/q/5265702/9157799#comment128297633_5265775 realpath -s "$@" | xclip fi # mark that you want to do a copy operation echo "copy" > ~/.copyOrCut > cut() < # use the previous function to save the paths to clipboard copy "$@" # but mark it as a cut operation echo "cut" >~/.copyOrCut > paste() < # for every path for ((i=1; i
If you don't know what .bashrc file is and never modify it before, just open the file explorer, go to Home, press Ctrl+H (show hidden files), search for .bashrc and open it with a text editor like gedit.
By using the above script, you are overriding the default functionality of these commands:
If you use one of those commands default functionality, just modify the script function names accordingly. For example, use p instead of paste .
Shell cut paste file in linux
Solution 3: You can implement this portably using file descriptors Tested with dash Question: I have 1344 rows of data column. Solution 1: Brief explanation, would match the first field which separated by commas, and behind refer to the match would be the remainding part except the first comma, and would refer to the match Solution 2: Shorter awk solution: gives: Explanation: EDIT: Reading your comments following your question, looks like your swapping more columns than just the first to the end of the line.
Linux cut, paste
I have to write a script file to cut the following column and paste it the end of the same row in a new .arff file. I guess the file type doesn't matter.
Current file:
63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,'50_1'
The output should be:
male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,'50_1',67
how can I do this? using a Linux script file?
- ^([^,]*) would match the first field which separated by commas, and \1 behind refer to the match
- (.*)$ would be the remainding part except the first comma, and \2 would refer to the match
Shorter awk solution:
male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,'50_1',67
EDIT: Reading your comments following your question, looks like your swapping more columns than just the first to the end of the line. You might consider using a swap function that you call multiple times:
However, this won't work whenever you want to move a column to the end of the line. So let's change that function:
$ cat tst.awk BEGIN 1 func swap(i,j)< s=$i if (j>NF) < for (k=i;kelse < $i=$j $j=s >>
$ awk -f tst.awk input.txt male,t,145,233,typ_angina,left_vent_hyper,150,no,2.3,down,0,fixed_defect,'50_1',67
Why using sed or awk, the shell can handle this easily
while read l;do f=$;echo $,$;done
If you want to keep the file in place.
printf "%s" "$(while read l;do f=$;printf "%s\n" "$,$";done infile
Awk - Linux cut, paste, I have to write a script file to cut the following column and paste it the end of the same row in a new .arff file. I guess the file type doesn't matter. Current file: 63,male,typ_angina,145,233,t, How do I prompt for Yes/No/Cancel input in a Linux shell script? 2063. How do I profile C++ code running on Linux? … Usage examplesed -r 's/^([^,]*),(.*)$/\2,\1/' Input_fileFeedback
Paste command in shell script
I'm trying to merge columns from two different files within the following script:
#!/bin/sh # # echo "1 1 1" > tmp1 echo "2 2 2" >> tmp1 echo "3 3 3" >> tmp1 echo "a,b,c" > tmp2 echo "a,b,c" >> tmp2 echo "a,b,c" >> tmp2 paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f 1-2 tmp2)
The above script works fine when I run
However, it does not work when I run
and I get the following error message
test.sh: line 13: syntax error near unexpected token `(' test.sh: line 13: `paste -d':' <(cut -d" " -f1 tmp1) <(cut -d"," -f 1-2 tmp2)'
Could somebody explain what is the reason of this behaviour? Is there fix it? Thx.
On your system, sh is presumably not set as bash ( dash may be?).
trap 'rm p1 p2' EXIT mkfifo p1 p2 cut -d " " -f1 tmp1 > p1 & cut -d " " -f 1-2 tmp2 > p2 & paste -d':' p1 p2
You can implement this portably using file descriptors
Linux -- How to cut a column from one file and paste, I would like to use the linux cut command to extract a column from a file, then use the paste command to insert the same column into a second file. I can do this by saving the results of the cut command, and then …
Cut and transpose some lines of a file
I have 1344 rows of data column. I want to form 32 row by 42 columns from my input data file. I want to cut the first 42 row and paste to one raw of 42 column data and continue down likewise. My data is arranged as
I want the result be output.txt
1 2 3 . 42 43 44 84 . . . 1303 1304 . . . 1344
I want you help me with a script that can do it.
perl -ne 'if($. % 42)else< print;>' data.txt
perl -pne 's/\n/ / if $. % 42' data.txt
The easiest way is to use rs command - if it is available:
COUNT=0 while read data do echo -n "$ " let COUNT=$+1; if (( $ % 42 == 0 )) then echo "" fi done < data.txt
If you could use python, numpy is quite well suited for this. Assuming a file x.txt contains the data, you could do something like:
#!/usr/bin/env python import numpy as np a=np.loadtxt('x.txt', dtype='int') # load array from file x.txt into variable a print a b=a.reshape(32,42) # reshape array to desired shape print b c=a.T # Do a transpose if required print c
Copy/Paste part of a file into another file using Terminal, I am trying to copy part of a .txt file from the line number n to the line number n+y (let's say 1000 to 1000000). I tried with operators and sed, and it failed. Here's the command I tried: sed -n "1000, 1000000p" path/first/file > path/second/file. bash shell unix terminal copy-paste. Share.
Cut, Copy, and Paste Files from the Command Line
I prefer to work from the command line, often switching between many terminals via something like tmux. I think it would be convenient to be able to cut, copy, and paste files between the current working directories (CWDs) in different terminals without having to write out the relative paths. For example, if the CWD of my terminal is /a/b/c/ and I want to copy a file called foo.txt to directory /d/e/f/ , then I have to write
Analogously, to move the file without copying (analogous to the "cut" verb of GUIs), I'd write
Writing out the whole relative path ../../../d/e/f is pretty annoying (did I get the number of .. right?), especially if I already have another terminal open with CWD /d/e/f/ . I would prefer to be able to somehow cut or copy the file in one terminal then paste it in another, similar to how GUI file managers typically work.
A while ago I wrote a Python tool to provide this functionality by copying files to and from an intermediate directory. However, I no longer use this tool because there are simpler methods, two of which I'll discuss here.
xclip
Probably the simplest method of all is to just use xclip, which comes with the commands
xclip-copyfile xclip-cutfile xclip-pastefile
which do exactly what we want. For convenience, I'd probably alias these to something shorter like
alias xcp=xclip-copyfile alias xmv=xclip-cutfile alias pst=xclip-pastefile
The names xcp and xmv are inspired both by x in xclip as well the great renameutils package, which provides the quick ( qcp , qmv ) and interactive ( icp , imv ) variants of cp and mv . These commands can be quite handy, so I'd recommend checking out renameutils if you haven't before.
One thing I don't like about this solution is that once a file has been cut using xclip-cutfile , it's stuck in the mysterious xclip clipboard. If I accidentally copy or cut something else before I paste it, then it's gone. Another (smaller) downside is that the file in the clipboard can only be pasted once, when I might like to copy the same file to multiple destinations.
A simple shell script
In an attempt to remedy the drawbacks of the xclip solution, I wrote a simple alternative shell script. Instead of the traditional order of cut or copy, then paste, I actually felt it to be more elegant to "mark" a set of files and then choose whether they should be copied or moved to the new location. Before I explain further, here is the script itself (you can also find it here):
CLIPBOARD_FILE=~/.xcp-clipboard # mark the file(s) for later use with cp or mv xmark() < realpath "$@" >"$CLIPBOARD_FILE" > # copy the marked files xcp() < local files while read line; do files+=("$line") done < "$CLIPBOARD_FILE" cp $files $@ ># move the marked files xmv()
To access these commands, you just need to copy the above code to your shell's rc file (like the .bashrc ) or otherwise source it.
Let's return to our example from the beginning of the post to see how these commands work. We have two terminal windows: Terminal #1 has CWD /a/b/c/ and Terminal #2 has CWD /d/e/f/ . The goal is to copy foo.txt from /a/b/c/ to /d/e/f/ without writing tedious relative paths. Making use of the functions in the above script, in Terminal #1 I can run:
which just "marks" the file by writing its full path /a/b/c/foo.txt to the clipboard file. Now, in Terminal #2, I can just run:
and the file will be copied from its original location to the CWD /d/e/f/ . Alternatively I could use xmv to move the file (i.e., deleting the original).
Looking at the definitions of the xcp and xmv functions above, one can see that they actually just call cp and mv respectively, except that the paths listed in the clipboard file are added as the first arguments. Any arguments supplied to xmv or xcp get passed along directly to mv or cp , respectively, which means we get to leverage all of the power of mv and cp for free. Want the command to print out what is being done? Pass -v (verbose). Want to avoid overwriting an existing file? Use -n (no-clobber). I was surprised by how many options there were when I read the man pages for for cp and mv : there is a lot going on there!
And that's it: as you can see, it is actually quite simple to cut and copy files from the command line. As usual, I'm interested in hearing about alternative solutions to the problem, as well as comments on the ones proposed above. The best way to reach me is by email.